﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using DataAccess;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System.Text;
using ATW.Bus;
using System.Data;
using BUS;
using System.Data.SqlClient;
using Com.Alipay;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Configuration;

namespace AnTeWei.AppInterface
{
    /// <summary>1
    /// AppPortal 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class AppPortal : System.Web.Services.WebService
    {
        CommonHelp _commonhelp = new CommonHelp();
        Contact _contact = new Contact();

        #region 八大模块接口

        #region 注册、完善App用户
        [WebMethod]
        //注册用户
        public string InsertAppUser(T_AppUser TAppUser)
        {
            int intResult = 0;
            string strSql = @"INSERT INTO T_AppUser
                            (tauType,tauLoginId,tauName,tauPsw,tauCountry,tauAdress,tauEmail,tauIsAuthorised
                            ,tauRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
                            ,tauAdditional1,tauAdditional2,tauAdditional3,tauAdditional4
                            ,tauAdditional5,tauAdditional6,tauAdditional7,tauAdditional8
                            ,tauAdditional9,tauAdditional10)
                            VALUES
                            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','0'
                            ,'0','system',GETDATE(),'','','{7}','{8}','{9}'
                            ,{10},'{11}','{12}','{13}','{14}','{15}','{16}')";
            strSql = string.Format(strSql, TAppUser.tauType, TAppUser.tauLoginId, TAppUser.tauName, TAppUser.tauPsw, TAppUser.tauCountry
                , TAppUser.tauAdress, TAppUser.tauEmail, TAppUser.tauAdditional1, TAppUser.tauAdditional2, TAppUser.tauAdditional3, 1
                , TAppUser.tauAdditional5, TAppUser.tauAdditional6, TAppUser.tauAdditional7, TAppUser.tauAdditional8, TAppUser.tauAdditional9, TAppUser.tauAdditional10);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(TAppUser.tauLoginId, TAppUser.tauPsw);

                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "注册App用户成功", "AshxPortalRequest - InsertAppUser");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "注册App用户失败", "AshxPortalRequest - InsertAppUser");
                return JsonConvert.SerializeObject(jRtn);
            }

        }

        [WebMethod]
        //完善用户
        public string UpdateAppUser(T_AppUser TAppUser)
        {
            int intResult = 0;
            string strSql = @"UPDATE T_AppUser
                            SET tauName='{0}',
	                            tauCountry ='{1}',
	                            tauAdress ='{2}',
                                tauEmail = '{3}',
                                UpdatedBy ='{4}',
                                UpdatedDate =GETDATE(),
                                tauAdditional1 = '{5}',
                                tauAdditional2  = '{6}',
                                tauAdditional3  = '{7}',
                                tauAdditional4  = {8},
                                tauAdditional5  = '{9}',
                                tauAdditional6  = '{10}',
                                tauAdditional7  = '{11}',
                                tauAdditional8  = '{12}',
                                tauAdditional9  = '{13}',
                                tauAdditional10  = '{14}'
                            WHERE tauType='{15}' and tauLoginId='{16}' and tauPsw='{17}'";

            strSql = string.Format(strSql, TAppUser.tauName, TAppUser.tauCountry
                , TAppUser.tauAdress, TAppUser.tauEmail, TAppUser.tauLoginId, TAppUser.tauAdditional1, TAppUser.tauAdditional2, TAppUser.tauAdditional3, 1
                , TAppUser.tauAdditional5, TAppUser.tauAdditional6, TAppUser.tauAdditional7, TAppUser.tauAdditional8, TAppUser.tauAdditional9, TAppUser.tauAdditional10
                , TAppUser.tauType, TAppUser.tauLoginId, TAppUser.tauPsw);

            
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(TAppUser.tauLoginId, TAppUser.tauPsw);


                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "完善App用户成功", "AshxPortalRequest - InsertAppUser");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "完善App用户失败", "AshxPortalRequest - InsertAppUser");
                return JsonConvert.SerializeObject(jRtn);
            }
        }

        #endregion

        #region 修改、重置密码
        [WebMethod]
        //修改密码
        public string UpdatePwd(string strLoginId, string strNewPwd, string strOldPwd)
        {
            int intResult = 0;
            string strSql = @"UPDATE T_AppUser
                            SET tauPsw='{0}'
                            WHERE tauLoginId='{1}' and tauPsw='{2}'";

            strSql = string.Format(strSql, strNewPwd, strLoginId, strOldPwd);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(strLoginId, strNewPwd);


                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "修改密码成功", "AshxPortalRequest - UpdatePwd");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "修改密码失败", "AshxPortalRequest - UpdatePwd");
                return JsonConvert.SerializeObject(jRtn);
            }
        }

        [WebMethod]
        //重置密码
        public string ResetPwd(string strLoginId, string strNewPwd)
        {
            int intResult = 0;
            string strSql = @"UPDATE T_AppUser
                            SET tauPsw='{0}'
                            WHERE tauLoginId='{1}'";

            strSql = string.Format(strSql, strNewPwd, strLoginId);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(strLoginId, strNewPwd);

                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "重置密码成功", "AshxPortalRequest - ResetPwd");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "重置密码失败", "AshxPortalRequest - ResetPwd");
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 短信验证
        public string GetMessage(string strLoginId, string strType)
        {
            int intResult = 0;

            strLoginId = strLoginId.Split('|')[0].ToString() + strLoginId.Split('|')[1].ToString();

            intResult = SMS.SendMessage(strLoginId, strType);
            //intResult = 666;

            if (intResult == -1)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "发送验证码失败", "AshxPortalRequest - GetMessage");
                return JsonConvert.SerializeObject(jRtn);

            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "发送验证码成功成功", "AshxPortalRequest - GetMessage");
                jRtn["data"] = JsonConvert.SerializeObject(intResult);
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 登录
        [WebMethod]
        public string Login(string strTauLoginId, string strTauPwd)
        {
            DataTable dt = new DataTable();

            dt = GetUserInfo(strTauLoginId, strTauPwd);

            if (dt == null || dt.Rows.Count == 0)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "登录失败,用户或密码错误", "AshxPortalRequest - Login");
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "登录成功", "AshxPortalRequest - Login");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }


        }
        #endregion

        #region 查询类别
        [WebMethod]
        public string SelectCategory(string strLoginId)
        {

            string strUrl = GetUrl();

            DataTable dt = new DataTable();
            if (string.IsNullOrWhiteSpace(strLoginId))
            {
                string strSql = @"select t1.TICID,t1.ticName,t1.ticDesc, '{0}'+t1.ticPictureUrl ticPictureUrl,isnull(t2.TicCount,0) TicCount, isnull(t3.SumPrice,0) SumPrice
                            from T_InformationCategory t1
                            left join
                            (
	                            select COUNT(*) TicCount,TicId from T_DemandInformation
	                            group by TicId
                            ) t2 on t1.TICID=t2.TicId
                            left join 
                            (
	                            select SUM(tt1.tdibTotalPrice) SumPrice,tt2.TicId
	                            from T_DemandInformationBid tt1
	                            inner join T_DemandInformation tt2 on tt1.TdiId=tt2.TDIID
	                            where tt2.tdiRowStatus=4
	                            group by tt2.TicId
                            ) t3 on t1.TICID=t3.TicId";

                strSql = string.Format(strSql, strUrl);
                dt = new DALTransaction().GetDataTableBySQLText(strSql);
            }
            else
            {
                string strSql = @"select t1.TICID,t1.ticName,t1.ticDesc, '{1}'+t1.ticPictureUrl ticPictureUrl,isnull(t2.TicCount,0) TicCount, isnull(t3.SumPrice,0) SumPrice
                            from T_InformationCategory t1
                            left join
                            (
	                            select COUNT(*) TicCount,TicId from T_DemandInformation
	                            group by TicId
                            ) t2 on t1.TICID=t2.TicId
                            left join 
                            (
	                            select SUM(tt1.tdibTotalPrice) SumPrice,tt2.TicId
	                            from T_DemandInformationBid tt1
	                            inner join T_DemandInformation tt2 on tt1.TdiId=tt2.TDIID
	                            where tt2.tdiRowStatus=4
	                            group by tt2.TicId
                            ) t3 on t1.TICID=t3.TicId
                            inner join T_AppUserAuthorised t4 on t1.TICID=t4.tauaTicId
                            inner join T_AppUser t5 on t4.tauaTauId=t5.TAUID
                            where t5.tauLoginId='{0}'";

                strSql = string.Format(strSql, strLoginId, strUrl);
                dt = new DALTransaction().GetDataTableBySQLText(strSql);
            }


            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询类别成功", "AshxPortalRequest - SelectCategory");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询子项
        [WebMethod]
        public string SelectItem(string strTicId)
        {
            string strSql = @"select * from D_Item where TICID={0}";

            strSql = string.Format(strSql, strTicId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询子项", "AshxPortalRequest - SelectItem");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询国别
        [WebMethod]
        public string SelectCountry()
        {
            string strSql = @"select * from D_Country where RowStatus!=2";

            strSql = string.Format(strSql);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询国别成功", "AshxPortalRequest - SelectCountry");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询省市区
        [WebMethod]
        public string SelectAddress()
        {
            string strSql = @"select * from D_Address";

            strSql = string.Format(strSql);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询省市区成功", "AshxPortalRequest - SelectAddress");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询基础参数
        [WebMethod]
        public string SelectFoundation(string strType)
        {
            string strUrl = GetUrl();

            string strSql = @"select DFID,
                    case dfType when 1 then  '{1}'+dfValue  else dfValue end value,
                    dfType,CreatedDate
                    from D_Foundation where dfType='{0}'";

            strSql = string.Format(strSql, strType, strUrl);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询基础参数成功", "AshxPortalRequest - SelectFoundation");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 发布需求
        [WebMethod]
        //无子项时发布
        public string DemandInformation(T_DemandInformation TDeInfoHead, string strLoginId)
        {
            return "";
//            try
//            {
//                try
//                {
//                    if (!string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional4))
//                    {
//                        //上传excel
//                        TDeInfoHead.tdiAdditional4 = UpdateExcelOrImg(TDeInfoHead.tdiAdditional4, "1");
//                    }
//                    if (!string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional5))
//                    {
//                        //上传img
//                        TDeInfoHead.tdiAdditional5 = UpdateExcelOrImg(TDeInfoHead.tdiAdditional5, "2");

//                    }
//                }
//                catch (Exception e)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "附件上传服务器异常", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }


//                int intResult = 0;

////                string strSqlHead = @"INSERT INTO T_DemandInformation
////                            (tdiNo,tauLoginId,tdiType,TicId,tdiTitle,tdiDesc,tdiPublishTime,tdiCloseTime
////                            ,tdiRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
////                            ,tdiAdditional1,tdiAdditional2,tdiAdditional3,tdiAdditional4
////                            ,tdiAdditional5,tdiAdditional6,tdiAdditional7,tdiAdditional8
////                            ,tdiAdditional9,tdiAdditional10,tdiBIdStartTime,tdiBIdEndTime)
////                            VALUES
////                            (newid(),'{0}','{1}','{2}','{3}','{4}',GETDATE(),dateadd(day,{5},GETDATE())
////                            ,'0','{6}',GETDATE(),'','','{7}','{8}','{9}'
////                            ,'{10}','{11}','{12}','{13}','{14}','{15}','{16}',dateadd(day,{5}-1,CONVERT(varchar(100), GETDATE(), 23)+' 10:00:00'),dateadd(day,{5}-1,CONVERT(varchar(100), GETDATE(), 23)+' 11:00:00'))";
////                strSqlHead = string.Format(strSqlHead, strLoginId, TDeInfoHead.tdiType, TDeInfoHead.TicId, TDeInfoHead.tdiTitle, TDeInfoHead.tdiDesc
////                    , TDeInfoHead.tdiCloseTime, strLoginId, TDeInfoHead.tdiAdditional1, TDeInfoHead.tdiAdditional2, TDeInfoHead.tdiAdditional3, TDeInfoHead.tdiAdditional4
////                    , TDeInfoHead.tdiAdditional5, TDeInfoHead.tdiAdditional6, TDeInfoHead.tdiAdditional7, TDeInfoHead.tdiAdditional8, TDeInfoHead.tdiAdditional9
////                    , TDeInfoHead.tdiAdditional10);

//                string strSqlHead = @"INSERT INTO T_DemandInformation
//                            (tdiNo,tauLoginId,tdiType,TicId,tdiTitle,tdiDesc,tdiPublishTime,tdiCloseTime
//                            ,tdiRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
//                            ,tdiAdditional1,tdiAdditional2,tdiAdditional3,tdiAdditional4
//                            ,tdiAdditional5,tdiAdditional6,tdiAdditional7,tdiAdditional8
//                            ,tdiAdditional9,tdiAdditional10)
//                            VALUES
//                            (newid(),'{0}','{1}','{2}','{3}','{4}',GETDATE(),dateadd(day,{5},GETDATE())
//                            ,'0','{6}',GETDATE(),'','','{7}','{8}','{9}'
//                            ,'{10}','{11}','{12}','{13}','{14}','{15}','{16}')";
//                strSqlHead = string.Format(strSqlHead, strLoginId, TDeInfoHead.tdiType, TDeInfoHead.TicId, TDeInfoHead.tdiTitle, TDeInfoHead.tdiDesc
//                    , TDeInfoHead.tdiCloseTime, strLoginId, TDeInfoHead.tdiAdditional1, TDeInfoHead.tdiAdditional2, TDeInfoHead.tdiAdditional3, TDeInfoHead.tdiAdditional4
//                    , TDeInfoHead.tdiAdditional5, TDeInfoHead.tdiAdditional6, TDeInfoHead.tdiAdditional7, TDeInfoHead.tdiAdditional8, TDeInfoHead.tdiAdditional9
//                    , TDeInfoHead.tdiAdditional10);

//                Log.Debug("sql发布需求", strSqlHead);
//                DBLog.WriteDBLog("监测sql", strSqlHead, "发布需求");
//                intResult = new DALTransaction().ExecuteQuerySQLText(strSqlHead);

//                if (intResult == 1)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("0", "发布需求成功", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//                else
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "发布需求失败", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//            }
//            catch (Exception ex)
//            {
//                JObject jRtn = new JObject();
//                DBLog.WriteDBLog("AshxPortalRequest - DemandInformation", ex.Message.ToString(), "发布需求失败");
//                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - DemandInformation");
//                return JsonConvert.SerializeObject(jRtn);
//            }

        }

        [WebMethod]
        //有子项时发布
        public string DemandInformation(T_DemandInformation TDeInfoHead, List<T_DemandInformationDetail> TDeInfoList, string strLoginId)
        {
            return "";
//            try
//            {
//                try
//                {
//                    if (!string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional4))
//                    {
//                        //上传excel
//                        TDeInfoHead.tdiAdditional4 = UpdateExcelOrImg(TDeInfoHead.tdiAdditional4, "1");
//                    }
//                    if (!string.IsNullOrWhiteSpace(TDeInfoHead.tdiAdditional5))
//                    {
//                        //上传img
//                        TDeInfoHead.tdiAdditional5 = UpdateExcelOrImg(TDeInfoHead.tdiAdditional5, "2");
                        
//                    }

//                }
//                catch (Exception e)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "附件上传服务器异常", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }

//                int intResult = 0;

//                List<string> lsSql = new List<string>();

//                string strNewid = Common.GetNewId();

                

////                string strSqlHead = @"INSERT INTO T_DemandInformation
////                            (tdiNo,tauLoginId,tdiType,TicId,tdiTitle,tdiDesc,tdiPublishTime,tdiCloseTime
////                            ,tdiRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
////                            ,tdiAdditional1,tdiAdditional2,tdiAdditional3,tdiAdditional4
////                            ,tdiAdditional5,tdiAdditional6,tdiAdditional7,tdiAdditional8
////                            ,tdiAdditional9,tdiAdditional10,tdiBIdStartTime,tdiBIdEndTime)
////                            VALUES
////                            ('{0}','{1}','{2}',{3},'{4}','{5}',GETDATE(),dateadd(day,{6},GETDATE())
////                            ,'0','{7}',GETDATE(),'','','{8}','{9}','{10}'
////                            ,'{11}','{12}','{13}','{14}','{15}','{16}','{17}',dateadd(day,{6}-1,CONVERT(varchar(100), GETDATE(), 23)+' 10:00:00'),dateadd(day,{6}-1,CONVERT(varchar(100), GETDATE(), 23)+' 11:00:00'))";
//                string strSqlHead = @"INSERT INTO T_DemandInformation
//                            (tdiNo,tauLoginId,tdiType,TicId,tdiTitle,tdiDesc,tdiPublishTime,tdiCloseTime
//                            ,tdiRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
//                            ,tdiAdditional1,tdiAdditional2,tdiAdditional3,tdiAdditional4
//                            ,tdiAdditional5,tdiAdditional6,tdiAdditional7,tdiAdditional8
//                            ,tdiAdditional9,tdiAdditional10)
//                            VALUES
//                            ('{0}','{1}','{2}',{3},'{4}','{5}',GETDATE(),dateadd(day,{6},GETDATE())
//                            ,'0','{7}',GETDATE(),'','','{8}','{9}','{10}'
//                            ,'{11}','{12}','{13}','{14}','{15}','{16}','{17}')";
                

//                strSqlHead = string.Format(strSqlHead, strNewid, strLoginId, TDeInfoHead.tdiType, TDeInfoHead.TicId, TDeInfoHead.tdiTitle, TDeInfoHead.tdiDesc
//                    , TDeInfoHead.tdiCloseTime, strLoginId, TDeInfoHead.tdiAdditional1, TDeInfoHead.tdiAdditional2, TDeInfoHead.tdiAdditional3, TDeInfoHead.tdiAdditional4
//                    , TDeInfoHead.tdiAdditional5, TDeInfoHead.tdiAdditional6, TDeInfoHead.tdiAdditional7, TDeInfoHead.tdiAdditional8, TDeInfoHead.tdiAdditional9
//                    , TDeInfoHead.tdiAdditional10);

//                Log.Debug("sql发布需求", strSqlHead);
//                lsSql.Add(strSqlHead);

//                foreach (var i in TDeInfoList)
//                {
//                    string strSqlList = @"INSERT INTO T_DemandInformationDetail
//                            (tdiNo,DiId,tdidQty,CreatedBy,CreatedDate)
//                            VALUES
//                            ('{0}',{1},{2},'{3}',GETDATE())";
//                    strSqlList = string.Format(strSqlList, strNewid, i.DiId, i.tdidQty, strLoginId);
                    
//                    lsSql.Add(strSqlList);
//                }

//                intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

//                if (intResult == 1)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("0", "发布需求成功", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//                else
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "发布需求失败", "AshxPortalRequest - DemandInformation");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//            }
//            catch (Exception ex)
//            {
//                JObject jRtn = new JObject();
//                DBLog.WriteDBLog("AshxPortalRequest - DemandInformation", ex.Message.ToString(), "发布需求失败");
//                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - DemandInformation");
//                return JsonConvert.SerializeObject(jRtn);
//            }

        }
        #endregion

        #region 查询需求
        [WebMethod]
        public string SelectInformation(string strType)
        {
            //type=1 ==>首页查询
            if (strType == "1")
            {
//                string strSql = @"select top 10 * from 
//                                (
//	                                select row_number() over(partition by t1.TicId,t1.tdiType order by t1.CreatedDate desc) as rownum
//	                                , t1.*,t2.ticName,isnull(tt.cc,0) Count
//	                                from T_DemandInformation t1
//	                                left join T_InformationCategory t2  on t1.TicId=t2.TICID
//	                                left join 
//	                                (
//		                                select count(1) cc,t3.tdiNo from T_DemandInformationBid t3
//		                                group by t3.tdiNo
//	                                ) tt on t1.tdiNo=tt.tdiNo 
//	                                where tdiRowStatus=1
//                                ) as T";

                string strSql = @"select top 10 * from 
                                (
                                   select t1.tdiTitle,t1.tdiDesc,t1.tdiBIdStartTime,tt.diNo,tt.diName
                                   from T_DemandInformation t1
                                   inner join 
                                   (select t.* from
	                                (select t2.tdiNo,t2.diNo,t3.diName,row_number() over (partition by tdiNo order by t2.DetailId desc) rn 
	                                from T_DemandInformationDetail t2
	                                inner join D_Item t3 on t2.diNo=t3.diNo) t
	                                where rn=1) tt on t1.tdiNo=tt.tdiNo
                                ) as T";

                strSql = string.Format(strSql, strType);

                DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);


                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "查询基础参数成功", "AshxPortalRequest - SelectFoundation");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "查询基础参数失败,Type不正确", "AshxPortalRequest - SelectFoundation");
                return JsonConvert.SerializeObject(jRtn);
            }

        }
        #endregion

        #region 根据tdiNo查询需求明细
        [WebMethod]
        public string SelectInformationList(string strTdiNo)
        {
            string strUrl = GetUrl();

//            string strSql = @"select t1.tdiNo,t1.TDIDID,t2.ticid,t2.tdiTitle,t2.tdiDesc,datediff(second,GETDATE(), t2.tdicloseTime) diffSecond,t2.tdiType,t2.tauLoginId,
//                    t1.DiId,t3.diDesc,t3.diName,t3.diUnit,t1.tdidQty,t2.tdiPublishTime,t2.tdiAdditional3,t1.tdidQty,
//                    substring(t2.tdiAdditional5,charindex('|',t2.tdiAdditional5)+1,len(t2.tdiAdditional5)-charindex(',',t2.tdiAdditional5)) tdiAdditional5,
//                    t2.tdiCloseTime,'{1}' url
//                    from T_DemandInformationDetail t1
//                    inner join T_DemandInformation t2 on t1.tdiNo=t2.tdiNo
//                    inner join D_Item t3 on t1.DiId=t3.DIID 
//                    where t1.tdiNo='{0}' and t2.tdiRowStatus in (0,1,4)";

//            strSql = string.Format(strSql, strTdiNo,strUrl);
//            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@strTdiNo", strTdiNo);
            parm1.Add("@strUrl", strUrl);

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectInformationList", parm1);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "根据tdiNo查询需求明细", "AshxPortalRequest - SelectInformationList");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询竞标
        [WebMethod]
        public string SelectBid(string strType,string strLoginId)
        {
            DataTable dt = new DataTable();
            JObject jRtn = new JObject();
            try
            {
                //查询需求表头
                if (strType=="2")
                {
                    string strSql = @"select t.TDIID,t.tdiNo,t.tauLoginId,t.tdiType,t.TicId,t1.ticName,t.CreatedDate,
                                    t.tdiTitle,t.tdiDesc,t.tdiPublishTime,t.tdiCloseTime,
                                    case t.tdiRowStatus when 0 then '未审核' 
                                    when 1 then '审核通过'
                                    when 3 then '已到期'
                                    when 4 then '完成 ' end RowStatus,
                                    t.tdiAdditional1,t.tdiAdditional2,t.tdiAdditional3
                                    from T_DemandInformation t
                                    inner join T_InformationCategory t1 on t.TICID=t1.TICID
                                    where t.tauLoginId='{0}'";

                    strSql = string.Format(strSql, strLoginId);

                    dt = new DALTransaction().GetDataTableBySQLText(strSql);
                }
                //查询竞价表头
                else if (strType == "1")
                {
                    string strSql = @"select t.TDIBID,t.tdiNo,t.tdibNo,t.tauLoginId,t.tdibCurrency,t.tdibTotalPrice,t.CreatedDate,
                                    t.tdibDeliveryPeriod,t.tdibDeliveryPeriodUnit,t.tdibPayType,t1.tdiTitle,t1.tdiDesc,
                                    case t.tdibRowSatus when 0 then '未审核' 
                                    when 1 then '审核通过'
                                    when 3 then '已到期'
                                    when 4 then '完成 ' end RowStatus
                                    from T_DemandInformationBid t
                                    inner join T_DemandInformation t1 on t.tdiNo=t1.tdiNo
                                    where t.tauLoginId='{0}'";

                    strSql = string.Format(strSql, strLoginId);

                    dt = new DALTransaction().GetDataTableBySQLText(strSql);
                }
                else
                {
                    
                    jRtn = Common.Return("-1", "类型不正确", "AshxPortalRequest - SelectBid");
                    return JsonConvert.SerializeObject(jRtn);
                }
               

                jRtn = Common.Return("0", "查询竞标成功", "AshxPortalRequest - SelectBid");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            catch(Exception ex)
            {
                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - SelectBid");
                return JsonConvert.SerializeObject(jRtn);
            }

        }
        #endregion

        #region 根据tdibNo查询竞标明细
        [WebMethod]
        public string SelectBidList(string strTdiNo, string tauLoginId)
        {
            try 
            {
                string strUrl = GetUrl();

//                string strSql = @"select t1.TDIBDID
//                                ,t1.tdibNo
//                                ,t1.TdibId
//                                ,t1.TdidId
//                                ,t1.tdibdPrice
//                                ,case t2.tdibRowSatus when  '0' then '进行中'
//                                when  '1' then '中标' 
//                                when  '3' then '未中标'
//                                when  '4' then '流标'
//                                end RowSatus
//                                ,t3.DiId,t4.diName,t4.diDesc,t4.diUnit
//                                ,t3.tdidQty,t5.tdiTitle,t5.tdiDesc,t5.tdiType
//                                ,t5.TicId,t6.ticName,t5.tdiAdditional1,t5.tdiAdditional2,t5.tdiAdditional3
//                                ,t5.CreatedDate,t5.tdiCloseTime,datediff(second,GETDATE(), t5.tdiCloseTime) diffSecond
//                                ,t2.tdibCurrency,t2.tdibDeliveryPeriod,t2.tdibDeliveryPeriodUnit,t2.tdibTotalPrice
//                                ,substring(t5.tdiAdditional5,charindex('|',t5.tdiAdditional5)+1,len(t5.tdiAdditional5)-charindex(',',t5.tdiAdditional5)) tdiAdditional5
//                                ,'{1}' url,t1.tdibdPrice*t3.tdidQty tdiSumPrice
//                                from T_DemandInformationBidDetail t1
//                                inner join T_DemandInformationBid t2 on t1.tdibNo=t2.tdibNo
//                                inner join T_DemandInformationDetail t3 on t1.TdidId=t3.TDIDID
//                                inner join D_Item t4 on t3.DiId=t4.DIID
//                                inner join T_DemandInformation t5 on t2.tdiNo=t5.tdiNo
//                                inner join T_InformationCategory t6 on t5.TicId=t6.TICID
//                                where t1.tdibNo='{0}'";

//                strSql = string.Format(strSql, strTdibNo,strUrl);
//                DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

                Dictionary<string, object> parm1 = new Dictionary<string, object>();
                parm1.Add("@strTdiNo", strTdiNo);
                parm1.Add("@tauLoginId", tauLoginId);
                parm1.Add("@strUrl", strUrl);

                DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectBidList", parm1);

                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "根据tdibNo查询竞标明细成功", "AshxPortalRequest - SelectBidList");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            catch (Exception ex)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - SelectBidList");
                return JsonConvert.SerializeObject(jRtn);
            }
            
        }
        #endregion

        #region 根据模块类型查询需求
        [WebMethod]
        public string SelectByType(string strTdiType, string strStatus, string strOrderBy, string strTicId, string strCurrentPageIndex, string strPageSize, string strSearchKey, string strTdiId)
        {

            Dictionary<string, object> parm = new Dictionary<string, object>();
            //parm.Add("@tdiType", strTdiType);
            if (strStatus == "open")
            {
                strStatus = "0,1";
            }
            else
            {
                strStatus = "3,4";
            }
            parm.Add("@Status", strStatus);
            parm.Add("@OrderBy", strOrderBy);
           // parm.Add("@TicId", strTicId);
            parm.Add("@CurrentPageIndex", strCurrentPageIndex);
            parm.Add("@PageSize", strPageSize);
            parm.Add("@SearchKey", strSearchKey);
            //parm.Add("@TdiId", strTdiId);

            

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_Maintenance_DemandInformation_SelectInfo",parm);

            

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "根据模块类型查询需求", "AshxPortalRequest - SelectByType");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 投标
        [WebMethod]
        //无子项时投标
        public string PublishBid(T_DemandInformationBid TBidHead, string strLoginId)
        {
            return "";
//            try
//            {
//                try
//                {
//                    if (!string.IsNullOrWhiteSpace(TBidHead.tdibAdditional1))
//                    {
//                        //上传excel
//                        TBidHead.tdibAdditional1 = UpdateExcelOrImg(TBidHead.tdibAdditional1, "1");
//                    }

//                }
//                catch (Exception e)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "附件上传服务器异常", "AshxPortalRequest - PublishBid");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//                int intResult = 0;

//                string strSqlHead = @"INSERT INTO T_DemandInformationBid
//                            (tdibNo,tdiNo,tauLoginId,tdibCurrency,tdibTotalPrice,tdibDeliveryPeriod,tdibDeliveryPeriodUnit,tdibPayType
//                            ,tdibRowSatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
//                            ,tdibAdditional1,tdibAdditional2,tdibAdditional3,tdibAdditional4
//                            ,tdibAdditional5,tdibAdditional6,tdibAdditional7,tdibAdditional8
//                            ,tdibAdditional9,tdibAdditional10)
//                            VALUES
//                            (newid(),'{0}','{1}','{2}',{3},'{4}','{5}','{6}',0,'{7}',GETDATE(),'','','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}')";
//                strSqlHead = string.Format(strSqlHead, TBidHead.tdiNo, strLoginId, TBidHead.tdibCurrency, TBidHead.tdibTotalPrice, TBidHead.tdibDeliveryPeriod
//                                           , TBidHead.tdibDeliveryPeriodUnit, TBidHead.tdibPayType, strLoginId, TBidHead.tdibAdditional1, TBidHead.tdibAdditional2
//                                           , TBidHead.tdibAdditional3, TBidHead.tdibAdditional4, TBidHead.tdibAdditional5, TBidHead.tdibAdditional6, TBidHead.tdibAdditional7
//                                           , TBidHead.tdibAdditional8, TBidHead.tdibAdditional9, TBidHead.tdibAdditional10);

//                intResult = new DALTransaction().ExecuteQuerySQLText(strSqlHead);


//                if (intResult == 1)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("0", "竞标发布成功", "AshxPortalRequest - PublishBid");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//                else
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "竞标发布失败", "AshxPortalRequest - PublishBid");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//            }
//            catch (Exception ex)
//            {
//                JObject jRtn = new JObject();
//                DBLog.WriteDBLog("AshxPortalRequest - PublishBid", ex.Message.ToString(), "竞标发布失败");
//                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - PublishBid");
//                return JsonConvert.SerializeObject(jRtn);
//            }
        }

        [WebMethod]
        //有子项时竞标
        public string PublishBid(List<T_DemandInformationBidDetail> TBidList)
        {
            return "";
            //try
            //{

            //    int intResult = 0;

            //    List<string> lsSql = new List<string>();

            //    foreach (var i in TBidList)
            //    {
            //        string strSqlList = @"update T_DemandInformationBidDetail set Single_Price={0} , IS_Responsed=1,deliver_time={2},args07='{3}' where ResDetailID='{1}'";
            //        strSqlList = string.Format(strSqlList, i.Single_Price, i.ResDetailID, i.deliver_time, i.args07);

            //        lsSql.Add(strSqlList);
            //    }

            //    string strSqlList3 = @"update T_Response set rowstatus=1 where ResponseID=(select ResponseID from T_ResponseDetail where ResDetailID='{0}')";
            //    strSqlList3 = string.Format(strSqlList3, TBidList[0].ResDetailID);

            //    lsSql.Add(strSqlList3);

            //    intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

            //    if (intResult == 1)
            //    {
            //        //获取多语言提示
            //        string strTips = _commonhelp.GetTips("1", strLanId);
            //        JObject jRtn = new JObject();
            //        jRtn = Common.Return("0", strTips, "AshxPortalRequest - CreateRequest");
            //        return JsonConvert.SerializeObject(jRtn);
            //    }
            //    else
            //    {
            //        //获取多语言提示
            //        string strTips = _commonhelp.GetTips("2", strLanId);
            //        JObject jRtn = new JObject();
            //        jRtn = Common.Return("-1", strTips, "AshxPortalRequest - CreateRequest");
            //        return JsonConvert.SerializeObject(jRtn);
            //    }
            //}
            //catch (Exception ex)
            //{

            //    JObject jRtn = new JObject();
            //    DBLog.WriteDBLog("AshxPortalRequest - CreateRequest", ex.Message.ToString(), "发布询价失败");
            //    jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - CreateRequest");
            //    return JsonConvert.SerializeObject(jRtn);
            //}
            //try
            //{

//                //try
//                //{
//                //    if (!string.IsNullOrWhiteSpace(TBidHead.tdibAdditional1))
//                //    {
//                //        //上传excel
//                //        TBidHead.tdibAdditional1 = UpdateExcelOrImg(TBidHead.tdibAdditional1, "1");
//                //    }

//                //}
//                //catch (Exception e)
//                //{
//                //    JObject jRtn = new JObject();
//                //    jRtn = Common.Return("-1", "附件上传服务器异常", "AshxPortalRequest - PublishBid");
//                //    return JsonConvert.SerializeObject(jRtn);
//                //}

//                int intResult = 0;

//                List<string> lsSql = new List<string>();

//                string strNewid = Common.GetNewId();

//                string strSqlHead = @"INSERT INTO T_DemandInformationBid
//                            (tdibNo,tdiNo,tauLoginId,tdibCurrency,tdibTotalPrice,tdibDeliveryPeriod,tdibDeliveryPeriodUnit,tdibPayType
//                            ,tdibRowSatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate,tdibAdditional1,tdibAdditional2,tdibAdditional3,tdibAdditional4
//                            ,tdibAdditional5,tdibAdditional6,tdibAdditional7,tdibAdditional8
//                            ,tdibAdditional9,tdibAdditional10)
//                            VALUES
//                            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}',0,'{8}',GETDATE(),'','','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}')";
//                strSqlHead = string.Format(strSqlHead, strNewid, TBidHead.tdiNo, strLoginId, TBidHead.tdibCurrency, TBidHead.tdibTotalPrice, TBidHead.tdibDeliveryPeriod
//                                           , TBidHead.tdibDeliveryPeriodUnit, TBidHead.tdibPayType, strLoginId, TBidHead.tdibAdditional1, TBidHead.tdibAdditional2
//                                           , TBidHead.tdibAdditional3, TBidHead.tdibAdditional4, TBidHead.tdibAdditional5, TBidHead.tdibAdditional6, TBidHead.tdibAdditional7
//                                           , TBidHead.tdibAdditional8, TBidHead.tdibAdditional9, TBidHead.tdibAdditional10);
                
//                lsSql.Add(strSqlHead);

//                foreach (var i in TBidList)
//                {
//                    string strSqlList = @"INSERT INTO T_DemandInformationBidDetail
//                            (tdibNo,TdidId,tdibdPrice,tdibdRowSatus,CreatedBy,CreatedDate)
//                            VALUES
//                            ('{0}',{1},{2},0,'{3}',GETDATE())";
//                    strSqlList = string.Format(strSqlList, strNewid, i.TdidId, i.tdibdPrice, strLoginId);
                    
//                    lsSql.Add(strSqlList);
//                }

//                intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

//                if (intResult == 1)
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("0", "竞标发布成功", "AshxPortalRequest - PublishBid");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//                else
//                {
//                    JObject jRtn = new JObject();
//                    jRtn = Common.Return("-1", "竞标发布失败", "AshxPortalRequest - PublishBid");
//                    return JsonConvert.SerializeObject(jRtn);
//                }
//            }
//            catch (Exception ex)
//            {
//                JObject jRtn = new JObject();
//                DBLog.WriteDBLog("AshxPortalRequest - PublishBid", ex.Message.ToString(), "竞标发布失败");
//                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - PublishBid");
//                return JsonConvert.SerializeObject(jRtn);
//            }
        }
        #endregion

        #region 推送消息
        [WebMethod]
        //推送消息
        public string SendMessage(T_Message TMessage,string strType)
        {
            bool blResult;
            blResult=_commonhelp.SendMessage(TMessage, strType,new Dictionary<string,string>());
//            bool blIsSend;

//            int intResult = 0;

//            string strSql = @"INSERT INTO T_Message
//                            (tmType,tmfrom,tmto,tmTitle,tmMessage,RowStatus,CreatedBy,CreatedDate)
//                            VALUES
//                            ('{0}','{1}','{2}','{3}','{4}',0,'{1}',GETDATE())";
//            strSql = string.Format(strSql, TMessage.tmType, TMessage.tmfrom, TMessage.tmto, TMessage.tmTitle, TMessage.tmMessage);
//            intResult= new DALTransaction().ExecuteQuerySQLText(strSql);

//            #region 调用推送接口(待完善)

//            //1.极光推送 2.邮箱推送 3.两者都推送
//            switch (strType)
//            {
//                case "2":
//                    intResult = _commonhelp.SendEmailAsnyc(TMessage);
//                    break;
//            }

//            if (intResult != -1)
//            {
//                blIsSend = true;
//            }
//            else
//            {
//                blIsSend = false;
//            }
//            #endregion

//            if (intResult == 1 && blIsSend == true)
//            {
//                blResult = true;
//            }
//            else
//            {
//                blResult = false;
//            }

            

            if (blResult)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "推送消息成功", "AshxPortalRequest - SendMessage");
                //if (TMessage.tmType=="4")
                //{
                //    jRtn["data"] = JsonConvert.SerializeObject(intResult);
                //}
                
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "推送消息失败", "AshxPortalRequest - SendMessage");
                return JsonConvert.SerializeObject(jRtn);
            }

        }
        #endregion

        #region 查询消息记录
        [WebMethod]
        public string SelectMessage(string strLoginId)
        {
            //查询所有消息
            string strSql = @"select * from T_Message 
                            where tmfrom='{0}' or tmto='{1}'
                            order by CreatedDate desc";

            strSql = string.Format(strSql, strLoginId, strLoginId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            //查询系统消息未读
            string strSql1 = @"select isnull(count(*),0) SystemCount from T_Message 
                            where (tmfrom='{0}' or tmto='{1}') and tmType=6 and RowStatus=0";

            strSql1 = string.Format(strSql1, strLoginId, strLoginId);
            DataTable dt1 = new DALTransaction().GetDataTableBySQLText(strSql1);
            string strSystemCount = dt1.Rows[0]["SystemCount"].ToString();
               

            //查询项目消息未读
            string strSql2 = @"select isnull(count(*),0) ProjectCount from T_Message 
                            where (tmfrom='{0}' or tmto='{1}') and tmType!=6 and RowStatus=0";

            strSql2 = string.Format(strSql2, strLoginId, strLoginId);
            DataTable dt2 = new DALTransaction().GetDataTableBySQLText(strSql2);
            string strProjectCount = dt2.Rows[0]["ProjectCount"].ToString();

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询消息记录成功", "AshxPortalRequest - SelectMessage");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            jRtn["SystemCount"] = strSystemCount;
            jRtn["ProjectCount"] = strProjectCount;
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询消息是否已读
        [WebMethod]
        public string CheckMessage(string strTmId)
        {
            string strSql = @"select * from T_Message 
                            where TMID in ({0})";

            strSql = string.Format(strSql, strTmId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            string strSql1 = @"update T_Message set RowStatus=1
                            where TMID in ({0})";

            strSql1 = string.Format(strSql1, strTmId);
            new DALTransaction().ExecuteQuerySQLText(strSql1);

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询消息是否已读成功", "AshxPortalRequest - CheckMessage");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion


        #region 设置闹钟
        [WebMethod]
        //设置闹钟
        public string SetClock(T_DemandUserClock TClock)
        {
            int intResult = 0;
            string strSql = @"INSERT INTO T_DemandUserClock
           (tdiNo
           ,tauLoginId
           ,tducClockTime
           ,tducRowStatus
           ,CreatedDate
           ,tdiAdditional1
           ,tdiAdditional2
           ,tdiAdditional3
           ,tdiAdditional4
           ,tdiAdditional5
           ,tdiAdditional6
           ,tdiAdditional7
           ,tdiAdditional8
           ,tdiAdditional9
           ,tdiAdditional10)
                            VALUES
                            ('{0}','{1}','{2}','{3}',GETDATE(),'{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')";
            strSql = string.Format(strSql, TClock.tdiNo, TClock.tauLoginId, TClock.tducClockTime, 0, TClock.tdiAdditional1, TClock.tdiAdditional2, TClock.tdiAdditional3, TClock.tdiAdditional4
                , TClock.tdiAdditional5, TClock.tdiAdditional6, TClock.tdiAdditional7, TClock.tdiAdditional8, TClock.tdiAdditional9, TClock.tdiAdditional10);
            Log.Debug("SetClock", strSql);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "设置闹钟成功", "AshxPortalRequest - SetClock");
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "设置闹钟失败", "AshxPortalRequest - SetClock");
                return JsonConvert.SerializeObject(jRtn);
            }

        }

        //更新闹钟
        [WebMethod]
        public string UpdateClock(T_DemandUserClock TClock)
        {
            Log.Debug("UpdateClock","进去");
            int intResult = 0;
            string strSql = @"UPDATE T_DemandUserClock
   SET tducClockTime='{0}'
      ,tducRowStatus = '{1}'
      ,tdiAdditional1 =  '{2}'
      ,tdiAdditional2 =  '{3}'
      ,tdiAdditional3 =  '{4}'
      ,tdiAdditional4 =  '{5}'
      ,tdiAdditional5 =  '{6}'
      ,tdiAdditional6 =  '{7}'
      ,tdiAdditional7 =  '{8}'
      ,tdiAdditional8 =  '{9}'
      ,tdiAdditional9 =  '{10}'
      ,tdiAdditional10 = '{11}'
      WHERE tdiNo='{12}' and tauLoginId='{13}'";
            strSql = string.Format(strSql, TClock.tducClockTime, TClock.tducRowStatus, TClock.tdiAdditional1, TClock.tdiAdditional2, TClock.tdiAdditional3, TClock.tdiAdditional4
                , TClock.tdiAdditional5, TClock.tdiAdditional6, TClock.tdiAdditional7, TClock.tdiAdditional8, TClock.tdiAdditional9, TClock.tdiAdditional10, TClock.tdiNo, TClock.tauLoginId);
            Log.Debug("UpdateClock", strSql);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "设置闹钟成功", "AshxPortalRequest - SetClock");
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", "设置闹钟失败", "AshxPortalRequest - SetClock");
                return JsonConvert.SerializeObject(jRtn);
            }

        }
        #endregion


        #region 查询闹钟
        [WebMethod]
        //查询闹钟
        public string SelectClock(string strLoginId, string strtdiNo)
        {
            //查询所有消息
            string strSql = @"select * from T_DemandUserClock 
                            where tdiNo='{0}' and  tauLoginId='{1}'";

            strSql = string.Format(strSql, strtdiNo, strLoginId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            

            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询闹钟成功", "AshxPortalRequest - SelectClock");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion


        #region 我的参与
        [WebMethod]
        public string MyPart(string strLoginId)
        {


            string strSql = @"select tt1.tdiTitle,tt1.tdiDesc,tt1.tdiBIdStartTime,tt.diNo,tt.diName
                            from T_DemandInformationBid t1
                            inner join 
                            (select t.* from
                            (select t4.tdiNo,t2.diNo,t3.diName,row_number() over (partition by tdiNo order by t2.DetailId desc) rn 
                            from T_DemandInformationBidDetail t2
                            inner join T_DemandInformationBid t4 on t2.BidNo=t4.BidNo
                            inner join D_Item t3 on t2.diNo=t3.diNo) t
                            where rn=1) tt on t1.tdiNo=tt.tdiNo
                            inner join T_DemandInformation tt1 on t1.tdiNo=tt1.tdiNo
                            inner join T_SupplierInfo t5 on t1.SupplierNo=t5.tauLoginId
                            where t5.tauLoginId='{0}'";

                strSql = string.Format(strSql, strLoginId);

                DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);


                JObject jRtn = new JObject();
                jRtn = Common.Return("0", "查询我的参与成功", "AshxPortalRequest - SelectFoundation");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 我的合同
        [WebMethod]
        public string PurchaseContract(string strLoginId)
        {


            string strSql = @"select t2.Title,t1.Seqno,t1.PContractID,t1.create_time
                            from T_PurchaseContract t1
                            inner join T_DemandInformationBid t2 on t1.TDIBID=t2.BidId
                            inner join T_SupplierInfo t3 on t2.SupplierNo=t3.SupplierNo
                            where t3.tauLoginId='{0}'";

            strSql = string.Format(strSql, strLoginId);

            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);


            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询我的合同成功", "PurchaseContract - PurchaseContract");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 合同详情
        [WebMethod]
        public string ContractDetail(string PContractID)
        {


            string strSql = @"select t1.*
                            from T_PurchaseContract t1
                            where t1.PContractID='{0}'";

            strSql = string.Format(strSql, PContractID);

            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);


            JObject jRtn = new JObject();
            jRtn = Common.Return("0", "查询合同详情成功", "ContractDetail - ContractDetail");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #endregion

        #region 销售端接口

        #region 登录
        [WebMethod]
        public string SalesLogin(string strLanId, string strTauLoginId, string strTauPwd)
        {
            DataTable dt = new DataTable();

            dt = GetUserInfo(strTauLoginId, strTauPwd);

            if (dt == null || dt.Rows.Count == 0)
            {
                //操作失败
                string strTips = _commonhelp.GetTips("2", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", strTips, "AshxPortalRequest - InsertSalesAppUser");
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                //操作成功
                string strTips = _commonhelp.GetTips("1", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", strTips, "AshxPortalRequest - InsertSalesAppUser");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }


        }
        #endregion

        #region 销售app注册
        


        [WebMethod]
        //注册用户
        public string InsertSalesAppUser(string strLanId,T_AppUser TAppUser)
        {
            int intResult = 0;

            //string strLoginId = TAppUser.tauLoginId.Split('|')[1].ToString();

            string strSql = @"INSERT INTO T_AppUser
                            (tauType,tauLoginId,tauName,tauPsw,tauCountry,tauAdress,tauEmail,tauIsAuthorised
                            ,tauRowStatus,CreatedBy,CreatedDate,UpdatedBy,UpdatedDate
                            ,tauAdditional1,tauAdditional2,tauAdditional3,tauAdditional4
                            ,tauAdditional5,tauAdditional6,tauAdditional7,tauAdditional8
                            ,tauAdditional9,tauAdditional10)
                            VALUES
                            ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','0'
                            ,'0','system',GETDATE(),'','','{7}','{8}','{9}'
                            ,{10},'{11}','{12}','{13}','{14}','{15}','{16}')";
            strSql = string.Format(strSql, TAppUser.tauType, TAppUser.tauLoginId, TAppUser.tauName, TAppUser.tauPsw, TAppUser.tauCountry
                , TAppUser.tauAdress, TAppUser.tauEmail, TAppUser.tauAdditional1, TAppUser.tauAdditional2, TAppUser.tauAdditional3, 1
                , TAppUser.tauAdditional5, TAppUser.tauAdditional6, TAppUser.tauAdditional7, TAppUser.tauAdditional8, TAppUser.tauAdditional9, TAppUser.tauAdditional10);
            Log.Debug("InsertSalesAppUser", strSql);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(TAppUser.tauLoginId, TAppUser.tauPsw);

                //更新分供商邀请码状态
                if (TAppUser.tauType == "3")
                {
                    UpdateSupplyCode(TAppUser.tauAdditional2);
                }

                //操作成功
                string strTips = _commonhelp.GetTips("1", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", strTips, "AshxPortalRequest - InsertSalesAppUser");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                //操作失败
                string strTips = _commonhelp.GetTips("2", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", strTips, "AshxPortalRequest - InsertSalesAppUser");
                return JsonConvert.SerializeObject(jRtn);
            }

        }

        [WebMethod]
        //完善用户
        public string UpdateSalesAppUser(string strLanId,T_AppUser TAppUser)
        {
            int intResult = 0;
            string strSql = @"UPDATE T_AppUser
                            SET tauName='{0}',
	                            tauCountry ='{1}',
	                            tauAdress ='{2}',
                                tauEmail = '{3}',
                                UpdatedBy ='{4}',
                                UpdatedDate =GETDATE(),
                                tauAdditional9  = '{8}'
                            WHERE tauType='{5}' and tauLoginId='{6}' and tauPsw='{7}'";

            strSql = string.Format(strSql, TAppUser.tauName, TAppUser.tauCountry
                , TAppUser.tauAdress, TAppUser.tauEmail, TAppUser.tauLoginId
                , TAppUser.tauType, TAppUser.tauLoginId, TAppUser.tauPsw, TAppUser.tauAdditional9);

            Log.Debug("UpdateSalesAppUser", strSql);
            intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

            if (intResult == 1)
            {
                //操作成功后， 直接登录
                DataTable dt = new DataTable();

                dt = GetUserInfo(TAppUser.tauLoginId, TAppUser.tauPsw);

                //操作成功
                string strTips = _commonhelp.GetTips("1", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", strTips, "AshxPortalRequest - InsertAppUser");
                jRtn["data"] = JsonConvert.SerializeObject(dt);
                return JsonConvert.SerializeObject(jRtn);
            }
            else
            {
                //操作失败
                string strTips = _commonhelp.GetTips("2", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", strTips, "AshxPortalRequest - InsertAppUser");
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 获取系统参数(包括获取最新版本)
        [WebMethod]
        public string SelectSystemArgs(string strLanId,string strType)
        {
            string strSql = @"select * from D_SystemArgs 
                            where [key] = '{0}'";

            strSql = string.Format(strSql, strType);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectSystemArgs");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 产品参数
        [WebMethod]
        public string SelectProductionArgs(string strLanId, string strType, string strKey, string strProductionId)
        {
            string strUrl = GetUrl();
            //根据类型查询产品参数
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", strLanId);
            //type：1.产品T_Production 2.D_Material(材质表)(阀体材质) 3.D_PressureLevel(压力等级表) 4.D_Connection(连接方式表） 5.D_Drive(驱动表) 6.D_Size(口径表)
            //7.C_DesignLevel (设计标准表) 8.C_FranLevel(法兰标准表) 9.C_StructLevel(结构长度标准表) 10.C_SealLevel(密封测试要求)
            parm.Add("@Type", strType);
            parm.Add("@SearchKey", strKey);
            parm.Add("@ProductionId", strProductionId);
            parm.Add("@strUrl", strUrl);

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectProductionArgs", parm);

            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectSystemArgs");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 客户端首页查询
        [WebMethod]
        public string SelectClientFirst(string strLanId, string strLoginId, string strFromId)
        {
            string strToDoCount = "";
            string strPrice = "";
            //抓取待办事项
            string strSqlCount = @"select isnull(count(1),0) ToDoCount
                            from T_Request t1
                            where t1.tauloginid='{0}' and t1.rowstatus in (0,1,3,4,7)";

            strSqlCount = string.Format(strSqlCount, strLoginId);
            DataTable dtCount = new DALTransaction().GetDataTableBySQLText(strSqlCount);
            if (dtCount != null && dtCount.Rows.Count > 0)
            {
                strToDoCount = dtCount.Rows[0]["ToDoCount"].ToString();
            }
            //成交额
            string strSqlPrice = @"select isnull(SUM(t2.Buy_num*t2.finla_price_Cn),0) Price
                                from T_Request t1
                                inner join T_RequestDetail t2 on t1.RequestID=t2.RequestID
                                where t1.tauloginid='{0}' and t1.rowstatus in (3,4,5,6)";

            strSqlPrice = string.Format(strSqlPrice, strLoginId);
            DataTable dtPrice = new DALTransaction().GetDataTableBySQLText(strSqlPrice);
            if (dtPrice != null && dtPrice.Rows.Count > 0)
            {
                strPrice = dtPrice.Rows[0]["Price"].ToString();
            }

            //分页查询我的询价
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LoginId", strLoginId);
            parm.Add("@LanId", strLanId);
            parm.Add("@FromID", strFromId);



            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectClientFirst", parm);


            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectTodo");
            jRtn["Count"] = strToDoCount;
            jRtn["Price"] = strPrice;
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }

        #endregion

        #region 查询询价单明细
        [WebMethod]
        public string SelectRequestDetail(string strLanId,string strRequestId)
        {
            string strUrl = GetUrl();
            //分页查询我的询价
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", strLanId);
            parm.Add("@RequestId", strRequestId);
            parm.Add("@strUrl", strUrl);
            DataTable dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetailHead", parm);



            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", strLanId);
            parm1.Add("@RequestId", strRequestId);
            parm1.Add("@strUrl", strUrl);
            DataTable dtList = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion

            DataView dvHead = new DataView(dtHead);
            dvHead.Sort = "Oid desc";
            dtHead = dvHead.Table.Copy();

            DataView dvList = new DataView(dtList);
            dvList.Sort = "Id asc";
            dtList = dvList.Table.Copy();


            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectRequestDetail");
            jRtn["datahead"] = JsonConvert.SerializeObject(dtHead);
            jRtn["datalist"] = JsonConvert.SerializeObject(dtList);

            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 确认报价
        [WebMethod]
        public string ConfirmResponse(string strLanId, List<T_ResponseDetail> ResponseList)
        {
            try
            {

                int intResult = 0;

                List<string> lsSql = new List<string>();

                //更新明细状态
                foreach (var i in ResponseList)
                {
                    string strSqlList = @"update T_ResponseDetail set  Is_Confirmed=1,rowstatus=6 where ResDetailID='{0}'";
                    strSqlList = string.Format(strSqlList, i.ResDetailID);
                    
                    lsSql.Add(strSqlList);

                    string strSqlList1 = @"update T_RequestDetail set Buy_num={0},rowstatus=6 where ReqDetailID='{1}'";
                    strSqlList1 = string.Format(strSqlList1, i.num, i.ReqDetailID);
                    
                    lsSql.Add(strSqlList1);
                }
                intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

                //获取相关参数
                string strRequestID = "";
                string strLoginId = "";
                string strTitle="";
                string strCountry = "";
                string strLan = "";
                DataTable dt = new DALTransaction().GetDataTableBySQLText(string.Format(@"select t1.RequestID,t1.tauloginid,t1.title,
                case when t2.tauCountry IS null OR t2.tauCountry ='' then '中国' else t2.tauCountry end tauCountry,
                ISNULL(t3.CountryLan,'en') CountryLan
                from T_RequestDetail t 
                inner join T_Request t1 on t.RequestID=t1.RequestID 
                inner join T_AppUser t2 on t1.tauloginid=t2.tauLoginId 
                left join D_Country t3 on t2.tauCountry=t3.dcuDesc
                where ReqDetailID='{0}'", ResponseList[0].ReqDetailID));
                if (dt == null || dt.Rows.Count == 0)
                    intResult = -1;

                strRequestID = dt.Rows[0]["RequestID"].ToString();
                strLoginId = dt.Rows[0]["tauloginid"].ToString();
                strTitle= dt.Rows[0]["title"].ToString();
                strCountry = dt.Rows[0]["tauCountry"].ToString();
                strLan = dt.Rows[0]["CountryLan"].ToString();

                //获取所有该询价单的报价
                DataTable dtRespone = new DALTransaction().GetDataTableBySQLText(string.Format(@"
                select *
                from T_Response where RequestID='{0}'", strRequestID));
               

                foreach (DataRow dr in dtRespone.Rows)
                {
                    if (CheckRespone(dr["ResponseID"].ToString()))
                    {//该供应商报价完全未被采用，推送告知供应商
                        Log.Debug("ConfirmResponse-2", dr["ResponseID"].ToString());
                        T_Message Entry = new T_Message();
                        Entry.tmType = "3";
                        Entry.tmfrom = "system";
                        Entry.tmTitle = "报价未被采用";

                        Entry.tmto = dr["tauloginid"].ToString();

                        Entry.tmMessage = "尊敬的供应商，很遗憾。您的报价单《" + strTitle + "》未被采用";

                        Entry.toType = "2";
                        Entry.toId = dr["ResponseID"].ToString();

                        Dictionary<string, string> Parm = new Dictionary<string, string>();
                        Parm.Add("ResponseId", dr["ResponseID"].ToString());
                        Log.Debug("ConfirmResponse-3", "发送" + dr["tauloginid"].ToString());
                        _commonhelp.SendMessage(Entry, "3", Parm); 
                    }
                }
                

                if (intResult == 1)
                {
                    Log.Debug("开始生成合同","001");

                    string strContactId = Common.GetNewId();
                    string strResult = "";
                    if (strCountry == "中国")
                    {
                        string strCusContactFilePath = Server.MapPath(ConfigurationManager.AppSettings["CusContactFilePath"]);
                        strResult = _contact.GenerateCustomerContact(strContactId, strRequestID, strCusContactFilePath);
                    }
                    else
                    {
                        string strCusContactFilePath = Server.MapPath(ConfigurationManager.AppSettings["EnCusContactFilePath"]);
                        strResult = _contact.GenerateCustomerContactEN(strContactId, strRequestID, strCusContactFilePath);
                    }
                    Log.Debug("开始生成合同3", strResult);
                    //生成客户合同成功，并推送
                    if ( strResult== "1")
                    {
                        #region 多语言
                            string strTip1 = "";
                            string strTip2 = "";
                            string strTip3 = "";
                            strTip1 = _commonhelp.GetTips("9", strLan);
                            strTip2 = _commonhelp.GetTips("10", strLan);
                            strTip3 = _commonhelp.GetTips("11", strLan);

                        #endregion
                        
                        
                        #region 申明实体，并赋值
                        T_Message Entry = new T_Message();
                        Entry.tmType = "2";
                        Entry.tmfrom = "system";
                        Entry.tmTitle = strTip1;

                        Entry.tmto = strLoginId;

                        Entry.tmMessage = strTip2 + strTitle + strTip3;

                        Entry.toType = "3";
                        Entry.toId = strContactId;

                        Dictionary<string, string> Parm = new Dictionary<string, string>();
                        Parm.Add("ContractId", strContactId);
                        Parm.Add("Type", "1");

                        _commonhelp.SendMessage(Entry, "3", Parm);
                        #endregion

                        //获取多语言提示
                        string strTips = _commonhelp.GetTips("1", strLanId);
                        JObject jRtn = new JObject();
                        jRtn = Common.Return("0", strTips, "AshxPortalRequest - CreateRequest");
                        return JsonConvert.SerializeObject(jRtn);
                    }
                    else
                    {
                        
                        //获取多语言提示
                        string strTips = _commonhelp.GetTips("2", strLanId);
                        JObject jRtn = new JObject();
                        jRtn = Common.Return("-1", strTips, "AshxPortalRequest - CreateRequest");
                        return JsonConvert.SerializeObject(jRtn);
                    }

                    
                }
                else
                {
                    //获取多语言提示
                    string strTips = _commonhelp.GetTips("2", strLanId);
                    JObject jRtn = new JObject();
                    jRtn = Common.Return("-1", strTips, "AshxPortalRequest - CreateRequest");
                    return JsonConvert.SerializeObject(jRtn);
                }
            }
            catch (Exception ex)
            {

                JObject jRtn = new JObject();
                DBLog.WriteDBLog("AshxPortalRequest - CreateRequest", ex.Message.ToString(), "确认报价失败");
                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - CreateRequest");
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 删除询价
        [WebMethod]
        public string DeleteRequest(string strLanId, string strRequestId)
        {
            string strSql = @"update T_Request set rowstatus='2' where RequestID='{0}'";

            strSql = string.Format(strSql, strRequestId);
            new DALTransaction().ExecuteQuerySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectRequestDetail");
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询合同
        [WebMethod]
        public string SelectContract(string strLanId, string strContractId)
        {
            string strSql = @"select t.*,t1.rowstatus RRowstatus 
from T_Contract t
inner join T_Request t1 on t.RequestID=t1.RequestID  where ContractID='{0}'";

            strSql = string.Format(strSql, strContractId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectContract");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 确认合同
        [WebMethod]
        public string ConfirmContract(string strLanId, string strContractId, string strType)
        {
            try
            {
                string strSql = "";
                int intResult = 0;
                if (strType == "1")
                {


                    string strRequestID = "";
                    string strtauloginid = "";

                    decimal ToTalPrice = 0;
                    strSql = @"select t.RequestID,t3.tauloginid,SUM(t4.Buy_num*t4.finla_price_Cn) ToTalPrice  from T_Contract t 
					inner join T_Request t3 on t.RequestID=t3.RequestID
				    inner join T_RequestDetail t4 on t.RequestID=t4.RequestID
                    where ContractID='{0}'
                    group by t.RequestID,t3.tauloginid";

                    Log.Debug("ConfirmContract-1", strContractId);

                    DataTable dt = new DALTransaction().GetDataTableBySQLText(string.Format(strSql, strContractId));
                    if (dt == null || dt.Rows.Count == 0)
                        intResult = -1;

                    strRequestID = dt.Rows[0]["RequestID"].ToString();
                    strtauloginid = dt.Rows[0]["tauloginid"].ToString();

                    ToTalPrice = Convert.ToDecimal(dt.Rows[0]["ToTalPrice"].ToString());

                    string sql = "";
                    decimal _limit = 0;
                    sql = "select [value] from D_SystemArgs where [key]='PrePayLimit'";
                    DataTable dto = new DALTransaction().GetDataTableBySQLText(sql.ToString());
                    if (dto != null && dto.Rows.Count > 0)
                    {
                        _limit = Convert.ToDecimal(dto.Rows[0]["value"].ToString());
                    }

                    string sql1 = "";
                    decimal _limit1 = 0;
                    sql = "select [value] from D_SystemArgs where [key]='ConcernPrice'";
                    DataTable dto1 = new DALTransaction().GetDataTableBySQLText(sql1.ToString());
                    if (dto1 != null && dto1.Rows.Count > 0)
                    {
                        _limit1 = Convert.ToDecimal(dto1.Rows[0]["value"].ToString());
                    }

                    //如果单笔满一定金额，推送销售，关怀客户
                    if (ToTalPrice > _limit1 * 10000)
                    {
                        string strLoginId = "";
                        //找到对应销售
                        string strSqlXi = @"select  isnull(t1.tauLoginId,'999') tauLoginId
                                from T_AppUser t 
                                left join T_AppUser t1 on t.tauAdditional3=t1.tauAdditional1
                                where t.tauLoginId='{0}' ";
                        strSqlXi = string.Format(strSqlXi, strtauloginid);
                        DataTable dtXi = new DALTransaction().GetDataTableBySQLText(strSqlXi.ToString());
                        if (dtXi != null && dtXi.Rows.Count > 0)
                        {
                            strLoginId = dtXi.Rows[0]["tauLoginId"].ToString();

                            //有对应销售，推送销售
                            if (strLoginId != "999")
                            {
                                #region 申明实体，并赋值
                                T_Message Entry = new T_Message();
                                Entry.tmType = "5";
                                Entry.tmfrom = "system";
                                Entry.tmTitle = "关怀提醒";
                                Entry.tmto = strLoginId;

                                Entry.tmMessage = "客户" + strtauloginid + "单笔采购单已满" + _limit1+"万元，请关怀。";

                                _commonhelp.SendMessage(Entry, "3", new Dictionary<string, string>());


                                #endregion 
                            }
                        }
                    }

                    if (ToTalPrice < _limit * 10000)
                    {

                        Log.Debug("ConfirmContract-2", "开始生成供应商合同");
                        strSql = @"update T_Contract set rowstatus=1 where ContractID='{0}'";

                        strSql = string.Format(strSql, strContractId);
                        Log.Debug("ConfirmContract-3", strSql);
                        intResult = new DALTransaction().ExecuteQuerySQLText(strSql);
                        //if (intResult == 1)
                        //{
                        //string strCusContactFilePath = ConfigurationManager.AppSettings["CusContactFilePath"];
                        string strSupContactFilePath = Server.MapPath(ConfigurationManager.AppSettings["SupContactFilePath"]);

                        string strResult = _contact.GenerateSupplyContact(strRequestID, strSupContactFilePath);
                        Log.Debug("Confirm321", strResult);
                        //生成分供商合同成功，并推送
                        if (strResult == "1")
                        {

                            //查询所有生成的分供商合同
                            strSql = @"select t.*,t1.tauloginid from T_Contract t
                                inner join T_Response t1 on t.ResponseID=t1.ResponseID
                                where t.RequestID='{0}' and type=2";
                            strSql = string.Format(strSql, strRequestID);
                            Log.Debug("Confirm123", strSql);
                            DataTable dt1 = new DALTransaction().GetDataTableBySQLText(strSql);

                            if (dt1 != null && dt1.Rows.Count > 0)
                            {
                                foreach (DataRow dr in dt1.Rows)
                                {
                                    string strLoginId = dr["tauloginid"].ToString();
                                    string strContactId = dr["ContractID"].ToString();
                                    Dictionary<string, string> Parm = new Dictionary<string, string>();
                                    Parm.Add("ContractId", strContactId);
                                    Parm.Add("Type", "2");

                                    #region 申明实体，并赋值
                                    T_Message Entry = new T_Message();
                                    Entry.tmType = "3";
                                    Entry.tmfrom = "system";
                                    Entry.tmTitle = "合同推送";

                                    Entry.tmto = strLoginId;

                                    Entry.tmMessage = "尊敬的供应商你好，您的报价已生成合同，请确认";

                                    Entry.toType = "3";
                                    Entry.toId = strContactId;

                                    _commonhelp.SendMessage(Entry, "3", Parm);
                                    #endregion
                                }
                            }





                            //获取多语言提示
                            string strTips = _commonhelp.GetTips("1", strLanId);
                            JObject jRtn = new JObject();
                            jRtn = Common.Return("0", strTips, "AshxPortalRequest - ConfirmContract");
                            return JsonConvert.SerializeObject(jRtn);
                        }
                        else
                        {
                            //获取多语言提示
                            string strTips = _commonhelp.GetTips("2", strLanId);
                            JObject jRtn = new JObject();
                            jRtn = Common.Return("-1", strTips, "AshxPortalRequest - ConfirmContract");
                            return JsonConvert.SerializeObject(jRtn);
                        }
                        //}
                        //else
                        //{
                        //    //获取多语言提示
                        //    string strTips = _commonhelp.GetTips("2", strLanId);
                        //    JObject jRtn = new JObject();
                        //    jRtn = Common.Return("-1", strTips, "AshxPortalRequest - ConfirmContract");
                        //    return JsonConvert.SerializeObject(jRtn);
                        //}
                    }
                    else
                    {
                        strSql = @"update T_Contract set rowstatus=3 where ContractID='{0}'";

                        strSql = string.Format(strSql, strContractId);
                        intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

                        if (intResult == 1)
                        {
                            //获取多语言提示
                            string strTips = _commonhelp.GetTips("1", strLanId);
                            JObject jRtn = new JObject();
                            jRtn = Common.Return("0", strTips, "AshxPortalRequest - ConfirmContract");
                            return JsonConvert.SerializeObject(jRtn);
                        }
                        else
                        {
                            //获取多语言提示
                            string strTips = _commonhelp.GetTips("2", strLanId);
                            JObject jRtn = new JObject();
                            jRtn = Common.Return("-1", strTips, "AshxPortalRequest - ConfirmContract");
                            return JsonConvert.SerializeObject(jRtn);
                        }
                    }
                }
                else
                {
                    strSql = @"update T_Contract set rowstatus=1 where ContractID='{0}'";

                    strSql = string.Format(strSql, strContractId);
                    intResult = new DALTransaction().ExecuteQuerySQLText(strSql);

                    if (intResult == 1)
                    {
                        //获取多语言提示
                        string strTips = _commonhelp.GetTips("1", strLanId);
                        JObject jRtn = new JObject();
                        jRtn = Common.Return("0", strTips, "AshxPortalRequest - ConfirmContract");
                        return JsonConvert.SerializeObject(jRtn);
                    }
                    else
                    {
                        //获取多语言提示
                        string strTips = _commonhelp.GetTips("2", strLanId);
                        JObject jRtn = new JObject();
                        jRtn = Common.Return("-1", strTips, "AshxPortalRequest - ConfirmContract");
                        return JsonConvert.SerializeObject(jRtn);
                    }
                }


            }
            catch (Exception ex)
            {
                Log.Debug("Confirm321",ex.Message.ToString());
                //获取多语言提示
                string strTips = _commonhelp.GetTips("2", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", strTips, "AshxPortalRequest - ConfirmContract");
                return JsonConvert.SerializeObject(jRtn);
            }

           
        }
        #endregion

        #region 我的待办查询
        [WebMethod]
        public string SelectTodo(string strLanId, string strLoginId,string strType)
        {
            DataTable dt = new DataTable();

            Dictionary<string,object> Parm=new Dictionary<string,object>();
            Parm.Add("@LoginId", strLoginId);
            Parm.Add("@Type", strType);

            dt = new DALTransaction().GetDataTableBySPName("SP_SelectTodo", Parm);
//            if (strType == "1")
//            {
//                string strSql = @"select distinct t1.RequestID,t1.title,t3.ContractID,t1.rowstatus,t3.content
//                            from T_Request t1
//                            left join T_Contract t3 on t1.RequestID=t3.RequestID and t3.type=1
//                            where t1.tauloginid='{0}' and t1.rowstatus in (0,1,3,4) ";

//                strSql = string.Format(strSql, strLoginId);
//                dt = new DALTransaction().GetDataTableBySQLText(strSql);
//            }
//            else
//            {
//                string strSql = @"select  distinct t1.RequestID,t1.title,t3.ContractID,t4.ResponseID,t3.content,t4.rowstatus
//                            from T_Request t1
//                            inner join T_Response t4 on t4.RequestID=t1.RequestID
//                            left join T_Contract t3 on t4.ResponseID=t3.ResponseID and t3.type=2
//                            where t4.tauloginid='{0}' and t4.rowstatus in (0,1,3,4) and t1.rowstatus in (0,1,3,4) ";

//                strSql = string.Format(strSql, strLoginId);
//                dt = new DALTransaction().GetDataTableBySQLText(strSql);
//            }
           

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectTodo");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 我的成交
        [WebMethod]
        public string SelectDone(string strLanId, string strLoginId, string strType, string strFromId)
        {

            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LoginId", strLoginId);
            parm.Add("@LanId", strLanId);
            parm.Add("@Type", strType);
            parm.Add("@FromID", strFromId);

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectDone", parm);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectTodo");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 发布询价
        [WebMethod]
        public string CreateRequest(string strLanId, T_Request RequestHead, List<T_RequestDetail> RequestList)
        {
            try
            {

                int intResult = 0;

                List<string> lsSql = new List<string>();

                string strNewid = Common.GetNewId();

                string strSqlHead = @"INSERT INTO T_Request
                                   (RequestID
                                   ,title
                                   ,tauloginid
                                   ,rowstatus
                                   ,create_time)
                             VALUES
                                   ('{0}','{1}','{2}',0,getdate())";

                

                strSqlHead = string.Format(strSqlHead, strNewid, RequestHead.title, RequestHead.tauloginid);

                
                lsSql.Add(strSqlHead);

                foreach (var i in RequestList)
                {
                    string strSqlList = @"INSERT INTO T_RequestDetail
                                       (RequestID
                                       ,ProductionID
                                       ,MID_B
                                       ,MID_S
                                       ,MID_I
                                       ,SID
                                       ,PressureID
                                       ,DriveID
                                       ,ConnID
                                       ,args01
                                       ,args02
                                       ,args03
                                       ,args04
                                       ,args05
                                       ,args06
                                       ,args07
                                       ,args08
                                       ,args09
                                       ,args010
                                       ,Query_num
                                       ,Buy_num
                                       ,deadLine
                                       ,rowstatus
                                       ,create_time)
                                 VALUES
                                       ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}',
                                        '{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}',{19},{20},{21},0,getdate())";
                    strSqlList = string.Format(strSqlList, strNewid, i.ProductionID, i.MID_B, i.MID_S, i.MID_I, i.SID, i.PressureID, i.DriveID
                        , i.ConnID, i.args01, i.args02, i.args03, i.args04, i.args05, i.args06, i.args07, i.args08, i.args09, i.args010
                        , i.Query_num, i.Query_num, i.deadLine);
                    
                    lsSql.Add(strSqlList);
                }

                intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

                if (intResult == 1)
                {
                    //生成报价数据并推送
                    _commonhelp.CreateResAndSendMess(strNewid);

                    //获取多语言提示
                    string strTips = _commonhelp.GetTips("1", strLanId);
                    JObject jRtn = new JObject();
                    jRtn = Common.Return("0", strTips, "AshxPortalRequest - CreateRequest");
                    return JsonConvert.SerializeObject(jRtn);
                }
                else
                {
                    //获取多语言提示
                    string strTips = _commonhelp.GetTips("2", strLanId);
                    JObject jRtn = new JObject();
                    jRtn = Common.Return("-1", strTips, "AshxPortalRequest - CreateRequest");
                    return JsonConvert.SerializeObject(jRtn);
                }
            }
            catch (Exception ex)
            {
                
                JObject jRtn = new JObject();
                DBLog.WriteDBLog("AshxPortalRequest - CreateRequest", ex.Message.ToString(), "发布询价失败");
                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - CreateRequest");
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 热门搜索查询
        [WebMethod]
        public string SelectHot(string strLanId,string  strLoginId,string strHotNum)
        {
            string strUrl = GetUrl();
            string strSql = @"select top {2} * from 
                            (select  t1.ProductionID,t2.[Desc] ProductionDesc,COUNT(t1.ProductionID) PCount,
                            '{3}'+t5.imgUrl imgUrl
                            from T_RequestDetail t1
                            inner join T_Production_MultiLan t2 on t1.ProductionID=t2.ProductionID
                            inner join T_Production t5 on t1.ProductionID=t5.ProductionID
                            inner join T_Request t3 on t1.RequestID=t3.RequestID
                            left join T_Response t4 on t3.RequestID=t4.RequestID
                            where t2.LanId='{0}' and (t3.tauloginid='{1}' or t4.tauloginid='{1}')
                            group by t1.ProductionID,t2.[Desc],'{3}'+t5.imgUrl) t
                            order by t.PCount desc";

            strSql = string.Format(strSql, strLanId, strLoginId, strHotNum, strUrl);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectHot");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }

        #endregion

        #region 分供商首页查询
        [WebMethod]
        public string SelectSupFirst(string strLanId, string strLoginId, string strFromId)
        {
            string strToDoCount = "";
            string strPrice = "";
            //抓取待办事项
            string strSqlCount = @"select isnull(count(1),0) ToDoCount
                                from T_Response t1
                                inner join T_Request t2 on t1.RequestID=t2.RequestID
                                where t1.tauloginid='{0}' and t1.rowstatus in (0,1,3,4,7) and t2.rowstatus in (0,1,3,4,7)";

            strSqlCount = string.Format(strSqlCount, strLoginId);
            DataTable dtCount = new DALTransaction().GetDataTableBySQLText(strSqlCount);
            if (dtCount != null && dtCount.Rows.Count > 0)
            {
                strToDoCount = dtCount.Rows[0]["ToDoCount"].ToString();
            }
            //成交额
            string strSqlPrice = @"select isnull(SUM(t2.Buy_num*t1.Single_Price),0) Price
				from T_ResponseDetail t1
				inner join T_RequestDetail t2 on t1.ReqDetailID=t2.ReqDetailID
				inner join T_Response t3 on t3.ResponseID=t1.ResponseID
				where   t1.Is_Confirmed=1 and t3.tauloginid='{0}'";

            strSqlPrice = string.Format(strSqlPrice, strLoginId);
            DataTable dtPrice = new DALTransaction().GetDataTableBySQLText(strSqlPrice);
            if (dtPrice != null && dtPrice.Rows.Count > 0)
            {
                strPrice = dtPrice.Rows[0]["Price"].ToString();
            }

            //分页查询我的询价
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LoginId", strLoginId);
            parm.Add("@LanId", strLanId);
            parm.Add("@FromID", strFromId);


            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectSupFirst", parm);


            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectTodo");
            jRtn["Count"] = strToDoCount;
            jRtn["Price"] = strPrice;
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询报价单明细
        [WebMethod]
        public string SelectResponseDetail(string strLanId, string strResponseId)
        {
            string strUrl = GetUrl();
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LanId", strLanId);
            parm.Add("@ResponseId", strResponseId);
            parm.Add("@strUrl", strUrl);
            DataTable dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetailHead", parm);

            #region try
            //string strNewid = Common.GetNewId();
            //var query = from t in dtHead.AsEnumerable()
            //            group t by new { RequestID = t.Field<string>("RequestID"), 
            //                             title = t.Field<string>("title"), 
            //                             ProductionID = t.Field<string>("ProductionID"),
            //                             ProductionDesc = t.Field<string>("ProductionDesc"),
            //                             MID_B = t.Field<string>("MID_B"),
            //                             MID_BDesc = t.Field<string>("MID_BDesc"),
            //                             MID_I = t.Field<string>("MID_I"),
            //                             MID_IDesc = t.Field<string>("MID_IDesc"),
            //                             MID_S = t.Field<string>("MID_S"),
            //                             MID_SDesc = t.Field<string>("MID_SDesc"),
            //                             args02 = t.Field<string>("args02"),
            //                             args02Desc = t.Field<string>("args02Desc"),
            //                             args09 = t.Field<string>("args09"),
            //                             args09Desc = t.Field<string>("args09Desc"),
            //                             PressureID = t.Field<string>("PressureID"),
            //                             PressureIDDesc = t.Field<string>("PressureIDDesc"),
            //                             ConnID = t.Field<string>("ConnID"),
            //                             ConnDesc = t.Field<string>("ConnDesc"),
            //                             args01 = t.Field<string>("args01"),
            //                             args08 = t.Field<string>("args08"),
            //                             args03 = t.Field<string>("args03"),
            //                             deadLine = t.Field<string>("deadLine"),
            //                             imgUrl = t.Field<string>("imgUrl")
            //            }
            //            into g
            //            select new
            //            {
            //                Oid=strNewid,
            //                RequestID = g.Key.RequestID,
            //                title = g.Key.title,
            //                ProductionID = g.Key.ProductionID,
            //                ProductionDesc = g.Key.ProductionDesc,
            //                MID_B = g.Key.MID_B,
            //                MID_BDesc = g.Key.MID_BDesc,
            //                MID_I = g.Key.MID_I,
            //                MID_IDesc = g.Key.MID_IDesc,
            //                MID_S = g.Key.MID_S,
            //                MID_SDesc = g.Key.MID_SDesc,
            //                args02 = g.Key.args02,
            //                args02Desc = g.Key.args02Desc,
            //                args09 = g.Key.args09,
            //                args09Desc = g.Key.args09Desc,
            //                PressureID = g.Key.PressureID,
            //                PressureIDDesc = g.Key.PressureIDDesc,
            //                ConnID = g.Key.ConnID,
            //                ConnDesc = g.Key.ConnDesc,
            //                args01 = g.Key.args01,
            //                args08 = g.Key.args08,
            //                args03 = g.Key.args03,
            //                deadLine = g.Key.deadLine,
            //                imgUrl = g.Key.imgUrl
            //            };
            //List<string> liResult= query.ToList();
            #endregion
            

            Dictionary<string, object> parm1 = new Dictionary<string, object>();
            parm1.Add("@LanId", strLanId);
            parm1.Add("@ResponseId", strResponseId);
            parm1.Add("@strUrl", strUrl);
            DataTable dtList = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetail", parm1);
            dtList.Columns.Add("Oid");

            #region 匹配明细头、内容
            for (int i = 0; i < dtList.Rows.Count; i++)
            {
                for (int j = 0; j < dtHead.Rows.Count; j++)
                {
                    if (dtList.Rows[i]["ResponseID"].ToString() == dtHead.Rows[j]["ResponseID"].ToString() &&
                        dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                        dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                        dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                        dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                        dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                        dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                        dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                        dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                        dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                        dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                        dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                        dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                        dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                        dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                        dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                        dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                        dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                        dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                        dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                        dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                        dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                        dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                        dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                        dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                        dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                        dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                        dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                        dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                    {
                        dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                    }
                }

            }
            #endregion

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectRequestDetail");
            //jRtn["datahead"] = _commonhelp.ConvertDataTableToJSON(dtHead);
            //jRtn["datalist"] = _commonhelp.ConvertDataTableToJSON(dtList);


            DataView dvHead = new DataView(dtHead);
            dvHead.Sort = "Oid desc";
            dtHead = dvHead.Table.Copy();

            DataView dvList = new DataView(dtList);
            dvList.Sort = "Id asc";
            dtList = dvList.Table.Copy();


            jRtn["datahead"] = JsonConvert.SerializeObject(dtHead);
            jRtn["datalist"] = JsonConvert.SerializeObject(dtList);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 报价接口
        [WebMethod]
        public string CreateResponse(string strLanId, List<T_ResponseDetail> ResponseList)
        {
            try
            {

                int intResult = 0;

                List<string> lsSql = new List<string>();

                foreach (var i in ResponseList)
                {
                    string strSqlList = @"update T_ResponseDetail set Single_Price={0} , IS_Responsed=1,deliver_time={2},args07='{3}' where ResDetailID='{1}'";
                    strSqlList = string.Format(strSqlList, i.Single_Price, i.ResDetailID,i.deliver_time,i.args07);
                    
                    lsSql.Add(strSqlList);
                }

                string strSqlList3 = @"update T_Response set rowstatus=1 where ResponseID=(select ResponseID from T_ResponseDetail where ResDetailID='{0}')";
                strSqlList3 = string.Format(strSqlList3, ResponseList[0].ResDetailID);
                
                lsSql.Add(strSqlList3);

                intResult = new DALTransaction().ExecuteSqlTrans(lsSql);

                if (intResult == 1)
                {
                    //获取多语言提示
                    string strTips = _commonhelp.GetTips("1", strLanId);
                    JObject jRtn = new JObject();
                    jRtn = Common.Return("0", strTips, "AshxPortalRequest - CreateRequest");
                    return JsonConvert.SerializeObject(jRtn);
                }
                else
                {
                    //获取多语言提示
                    string strTips = _commonhelp.GetTips("2", strLanId);
                    JObject jRtn = new JObject();
                    jRtn = Common.Return("-1", strTips, "AshxPortalRequest - CreateRequest");
                    return JsonConvert.SerializeObject(jRtn);
                }
            }
            catch (Exception ex)
            {

                JObject jRtn = new JObject();
                DBLog.WriteDBLog("AshxPortalRequest - CreateRequest", ex.Message.ToString(), "发布询价失败");
                jRtn = Common.Return("-1", ex.Message.ToString(), "AshxPortalRequest - CreateRequest");
                return JsonConvert.SerializeObject(jRtn);
            }
        }
        #endregion

        #region 查询成交详情
        [WebMethod]
        public string SelectDoneList(string strLanId, string strRequestId,string strType)
        {
            DataTable dtHead = new DataTable();
            DataTable dtList = new DataTable();
            string strUrl = GetUrl();
            if (strType == "1")
            {
                //分页查询我的询价
                Dictionary<string, object> parm = new Dictionary<string, object>();
                parm.Add("@LanId", strLanId);
                parm.Add("@RequestId", strRequestId);
                parm.Add("@strUrl", strUrl);
                

                dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetailHead", parm);

                dtList = new DALTransaction().GetDataTableBySPName("SP_SelectRequestDetail", parm);
                dtList.Columns.Add("Oid");

                List<DetailPrice> List = new List<DetailPrice>();
                DetailPrice Entry = new DetailPrice();



                #region 匹配明细头、内容
                for (int i = 0; i < dtList.Rows.Count; i++)
                {
                    for (int j = 0; j < dtHead.Rows.Count; j++)
                    {
                        if (
                            dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                            dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                            dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                            dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                            dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                            dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                            dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                            dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                            dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                            dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                            dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                            dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                            dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                            dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                            dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                            dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                            dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                            dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                            dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                            dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                            dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                            dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                            dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                            dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                            dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                            dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                            dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                            dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                        {
                            dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                        }
                    }

                }

                foreach (DataRow dr in dtHead.Rows)
                {
                    Entry = new DetailPrice();
                    Entry.Oid = dr["Oid"].ToString();
                    Entry.XiaoJi = 0;
                    List.Add(Entry);
                }
                #endregion

                #region 重新计算小计和合计

                decimal delSumPrice = 0;

                foreach (var i in List)
                {
                    foreach (DataRow dr in dtList.Rows)
                    {
                        if (dr["Oid"].ToString() == i.Oid && dr["Is_Confirmed"].ToString() == "1")
                        {
                            i.XiaoJi = i.XiaoJi + (Convert.ToDecimal(dr["finla_price"]) * Convert.ToDecimal(dr["Buy_num"]));
                        }
                    }
                }

                foreach (var i in List)
                {
                    delSumPrice = delSumPrice + i.XiaoJi;
                }

                foreach (DataRow dr in dtHead.Rows)
                {
                    foreach (var i in List)
                    {
                        if (dr["Oid"].ToString() == i.Oid)
                        {
                            dr["Xiaoji"] = i.XiaoJi;
                            dr["SumPrice"] = delSumPrice;
                            Log.Debug("SelectDoneList", dr["Oid"].ToString());
                            Log.Debug("SelectDoneList", dr["Xiaoji"].ToString());
                            Log.Debug("SelectDoneList", dr["SumPrice"].ToString());

                        }
                    }
                }

                #endregion
            }
            else
            {
                Dictionary<string, object> parm = new Dictionary<string, object>();
                parm.Add("@LanId", strLanId);
                parm.Add("@ResponseId", strRequestId);
                parm.Add("@strUrl", strUrl);
                dtHead = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetailHead", parm);

                dtList = new DALTransaction().GetDataTableBySPName("SP_SelectResponseDetail", parm);
                dtList.Columns.Add("Oid");

                List<DetailPrice> List=new List<DetailPrice>();
                DetailPrice Entry=new DetailPrice();

                #region 匹配明细头、内容
                for (int i = 0; i < dtList.Rows.Count; i++)
                {
                    for (int j = 0; j < dtHead.Rows.Count; j++)
                    {
                        

                        if (dtList.Rows[i]["ResponseID"].ToString() == dtHead.Rows[j]["ResponseID"].ToString() &&
                            dtList.Rows[i]["RequestID"].ToString() == dtHead.Rows[j]["RequestID"].ToString() &&
                            dtList.Rows[i]["title"].ToString() == dtHead.Rows[j]["title"].ToString() &&
                            dtList.Rows[i]["ProductionID"].ToString() == dtHead.Rows[j]["ProductionID"].ToString() &&
                            dtList.Rows[i]["ProductionDesc"].ToString() == dtHead.Rows[j]["ProductionDesc"].ToString() &&
                            dtList.Rows[i]["MID_B"].ToString() == dtHead.Rows[j]["MID_B"].ToString() &&
                            dtList.Rows[i]["MID_BDesc"].ToString() == dtHead.Rows[j]["MID_BDesc"].ToString() &&
                            dtList.Rows[i]["MID_I"].ToString() == dtHead.Rows[j]["MID_I"].ToString() &&
                            dtList.Rows[i]["MID_S"].ToString() == dtHead.Rows[j]["MID_S"].ToString() &&
                            dtList.Rows[i]["MID_IDesc"].ToString() == dtHead.Rows[j]["MID_IDesc"].ToString() &&
                            dtList.Rows[i]["MID_SDesc"].ToString() == dtHead.Rows[j]["MID_SDesc"].ToString() &&
                            dtList.Rows[i]["args02"].ToString() == dtHead.Rows[j]["args02"].ToString() &&
                            dtList.Rows[i]["args02Desc"].ToString() == dtHead.Rows[j]["args02Desc"].ToString() &&
                            dtList.Rows[i]["args09"].ToString() == dtHead.Rows[j]["args09"].ToString() &&
                            dtList.Rows[i]["args09Desc"].ToString() == dtHead.Rows[j]["args09Desc"].ToString() &&
                            dtList.Rows[i]["args04"].ToString() == dtHead.Rows[j]["args04"].ToString() &&
                            dtList.Rows[i]["args04Desc"].ToString() == dtHead.Rows[j]["args04Desc"].ToString() &&
                            dtList.Rows[i]["args05"].ToString() == dtHead.Rows[j]["args05"].ToString() &&
                            dtList.Rows[i]["args06"].ToString() == dtHead.Rows[j]["args06"].ToString() &&
                            dtList.Rows[i]["args07"].ToString() == dtHead.Rows[j]["args07"].ToString() &&
                            dtList.Rows[i]["Level"].ToString() == dtHead.Rows[j]["Level"].ToString() &&
                            dtList.Rows[i]["PressureID"].ToString() == dtHead.Rows[j]["PressureID"].ToString() &&
                            dtList.Rows[i]["PressureIDDesc"].ToString() == dtHead.Rows[j]["PressureIDDesc"].ToString() &&
                            dtList.Rows[i]["ConnID"].ToString() == dtHead.Rows[j]["ConnID"].ToString() &&
                            dtList.Rows[i]["ConnDesc"].ToString() == dtHead.Rows[j]["ConnDesc"].ToString() &&
                            dtList.Rows[i]["args01"].ToString() == dtHead.Rows[j]["args01"].ToString() &&
                            dtList.Rows[i]["args08"].ToString() == dtHead.Rows[j]["args08"].ToString() &&
                            dtList.Rows[i]["args03"].ToString() == dtHead.Rows[j]["args03"].ToString() &&
                            dtList.Rows[i]["imgUrl"].ToString() == dtHead.Rows[j]["imgUrl"].ToString())
                        {
                            dtList.Rows[i]["Oid"] = dtHead.Rows[j]["Oid"].ToString();
                        }
                    }

                }

                foreach (DataRow dr in dtHead.Rows)
                {
                    Entry = new DetailPrice();
                    Entry.Oid = dr["Oid"].ToString();
                    Entry.XiaoJi = 0;
                    List.Add(Entry);
                }
                
                #endregion

                #region 重新计算小计和合计

                //Log.Debug("","");
                decimal delSumPrice = 0;

                //foreach (DataRow dr in dtList.Rows)
                //{
                //    foreach (var i in List)
                //    {
                //        if (dr["Oid"].ToString() == i.Oid && dr["Is_Confirmed"].ToString() == "1")
                //        {
                //            i.XiaoJi = i.XiaoJi + (Convert.ToDecimal(dr["Single_Price"]) * Convert.ToDecimal(dr["Buy_num"]));
                //        }
                //    }
                //}


                foreach (var i in List)
                {
                    foreach (DataRow dr in dtList.Rows)
                    {
                        if (dr["Oid"].ToString() == i.Oid && dr["Is_Confirmed"].ToString() == "1")
                        {
                            i.XiaoJi = i.XiaoJi + (Convert.ToDecimal(dr["Single_Price"]) * Convert.ToDecimal(dr["Buy_num"]));
                        }
                    }
                }

                foreach (var i in List)
                {
                    delSumPrice = delSumPrice + i.XiaoJi;
                }

                foreach (DataRow dr in dtHead.Rows)
                {
                    foreach (var i in List)
                    {
                        if (dr["Oid"].ToString() == i.Oid)
                        {
                            dr["Xiaoji"] = i.XiaoJi;
                            dr["SumPrice"] = delSumPrice;

                        }
                    }
                }

                #endregion

            }

            





            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectRequestDetail");
            jRtn["datahead"] = JsonConvert.SerializeObject(dtHead);
            jRtn["datalist"] = JsonConvert.SerializeObject(dtList);

            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询供货范围
        [WebMethod]
        public string SelectSupplyRange(string strLanId, string strLoginId)
        {
            //查询供货范围
            Dictionary<string, object> parm = new Dictionary<string, object>();
            parm.Add("@LoginId", strLoginId);
            parm.Add("@LanId", strLanId);

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRange", parm);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("4", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectSupplyRange");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询供货范围明细
        [WebMethod]
        public string SelectSupplyRangeDetail(string strLanId, string strLoginId, string strProductionID)
        {
            Dictionary<string, object> parm = new Dictionary<string, object>();
            //T_Appuser_Material(用户X材质表) 
            parm = new Dictionary<string, object>();
            parm.Add("@Type", "1");
            parm.Add("@LoginId", strLoginId);
            parm.Add("@ProductionID", strProductionID);

            DataTable dtM = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRangeDetail", parm);

            //T_Appuser_Size(用户X口径)
            parm = new Dictionary<string, object>();
            parm.Add("@Type", "2");
            parm.Add("@LoginId", strLoginId);
            parm.Add("@ProductionID", strProductionID);
            DataTable dtS = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRangeDetail", parm);

            //T_Appuser_Pressure(用户X压力等级)
            parm = new Dictionary<string, object>();
            parm.Add("@Type", "3");
            parm.Add("@LoginId", strLoginId);
            parm.Add("@ProductionID", strProductionID);
            DataTable dtP = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRangeDetail", parm);

            //T_Appsuer_Seal(用户X密封测试要求)
            parm = new Dictionary<string, object>();
            parm.Add("@Type", "4");
            parm.Add("@LoginId", strLoginId);
            parm.Add("@ProductionID", strProductionID);
            DataTable dtSeal = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRangeDetail", parm);

            //T_Appuser_Temperature(用户X温度)
            parm = new Dictionary<string, object>();
            parm.Add("@Type", "5");
            parm.Add("@LoginId", strLoginId);
            parm.Add("@ProductionID", strProductionID);
            DataTable dtT = new DALTransaction().GetDataTableBySPName("SP_SelectSupplyRangeDetail", parm);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("4", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectSupplyRange");
            jRtn["dataMaterial"] = JsonConvert.SerializeObject(dtM);
            jRtn["dataSize"] = JsonConvert.SerializeObject(dtS);
            jRtn["dataPressure"] = JsonConvert.SerializeObject(dtP);
            jRtn["dataSeal"] = JsonConvert.SerializeObject(dtSeal);
            jRtn["dataTemperature"] = JsonConvert.SerializeObject(dtT);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 添加删除供货范围
        [WebMethod]
        public string EditSupplyRange(string strLanId, T_SupplyRange SupplyRange)
        {
            string strSql = @"INSERT INTO T_SupplyRange
                               (tauloginid
                               ,content
                               ,[type]
                               ,rowstatus
                               ,create_time)
                                VALUES
                               ('{0}','{1}','{2}',0,getdate())";

            strSql = string.Format(strSql, SupplyRange.tauloginid, SupplyRange.content, SupplyRange.type);
            
            new DALTransaction().ExecuteQuerySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - EditSupplyRange");
            return JsonConvert.SerializeObject(jRtn);
        } 

        #endregion

        #region 销售端首页
        [WebMethod]
        public string SelectSalesFirst(string strLanId, string strLoginId, string strFromId)
        {
            try
            {
                string strToDoCount = "";
                string strNum = "";
                //抓取待办事项
                string strSqlCount = @"select COUNT(1) ToDoCount
                                from T_AppUser t1
                                where t1.tauAdditional3 = 
                                (select tauAdditional1 from T_AppUser where tauLoginId='{0}')";

                strSqlCount = string.Format(strSqlCount, strLoginId);
                
                DataTable dtCount = new DALTransaction().GetDataTableBySQLText(strSqlCount);
                if (dtCount != null && dtCount.Rows.Count > 0)
                {
                    strToDoCount = dtCount.Rows[0]["ToDoCount"].ToString();
                }
                //我的客户
                string strSqlPrice = @"select COUNT(1) ClientNum
                                from T_AppUser t1
                                where t1.tauAdditional3 = 
                                (select tauAdditional1 from T_AppUser where tauLoginId='{0}')";

                strSqlPrice = string.Format(strSqlPrice, strLoginId);
                Log.Debug("strSqlPrice", strSqlPrice);
                DataTable dtPrice = new DALTransaction().GetDataTableBySQLText(strSqlPrice);
                if (dtPrice != null && dtPrice.Rows.Count > 0)
                {
                    strNum = dtPrice.Rows[0]["ClientNum"].ToString();
                }


                //查询销售排行
                string strSqlRank = @"select top 3 t6.tauLoginId,t6.tauName,SUM(t2.Buy_num*t2.finla_price) SumPrice
                                from T_Request t1
                                inner join T_RequestDetail t2 on t1.RequestID=t2.RequestID
                                inner join T_AppUser t5 on t1.tauloginid=t5.tauLoginId
                                inner join T_AppUser t6 on t5.tauAdditional3=t6.tauAdditional1
                                where t1.rowstatus!=2
                                group by t6.tauLoginId,t6.tauName
                                order by SumPrice desc";
                
                DataTable dtRank = new DALTransaction().GetDataTableBySQLText(strSqlRank);

                //查询最近五个月成交额数据
                Dictionary<string, object> parm1 = new Dictionary<string, object>();
                parm1.Add("@LoginId", strLoginId);

                DataTable dtMonthPrice = new DALTransaction().GetDataTableBySPName("SP_SqlMonthPrice", parm1);

                //查询最近五个月客户数量
                Dictionary<string, object> parm2 = new Dictionary<string, object>();
                parm2.Add("@LoginId", strLoginId);

                DataTable dtClientCount = new DALTransaction().GetDataTableBySPName("SP_SqlClientCount", parm2);


                //获取多语言提示
                string strTips = _commonhelp.GetTips("1", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectSalesFirst");
                jRtn["Count"] = strToDoCount;
                jRtn["ClientNum"] = strNum;
                jRtn["dataRank"] = JsonConvert.SerializeObject(dtRank);
                jRtn["dataMonthPrice"] = JsonConvert.SerializeObject(dtMonthPrice);
                jRtn["dataClientCount"] = JsonConvert.SerializeObject(dtClientCount);
                
                
                return JsonConvert.SerializeObject(jRtn);
            }
            catch (Exception ex)
            {
                Log.Debug("SelectSalesFirst", ex.Message.ToString());
                //获取多语言提示
                string strTips = _commonhelp.GetTips("2", strLanId);
                JObject jRtn = new JObject();
                jRtn = Common.Return("-1", strTips, "AshxPortalRequest - SelectSalesFirst");
                
                return JsonConvert.SerializeObject(jRtn);
                
            }
        }
        #endregion

        #region 查询客户
        [WebMethod]
        public string SelectClients(string strLanId, string strLoginId)
        {
            string strSql = @"select t1.*,tt1.create_time guanhuaiTime,tt2.create_time lianxiTime
                            from T_AppUser t1
                            inner join T_AppUser t2 on t1.tauAdditional3=t2.tauAdditional1 and t2.tauLoginId='{0}'
                            left join 
                            (	
	                            select t.* from
								(select T_Concern.*,row_number() over (partition by tauloginid,targetID order by create_time desc) rn 
								from T_Concern
								where type=1) t
								where rn=1
                            ) tt1 on tt1.tauloginid=t2.tauLoginId and tt1.targetID=t1.tauLoginId
                            left join 
                            (	
	                            select t.* from
								(select T_Concern.*,row_number() over (partition by tauloginid,targetID order by create_time desc) rn 
								from T_Concern
								where type=2) t
								where rn=1
                            ) tt2 on tt2.tauloginid=t2.tauLoginId and tt2.targetID=t1.tauLoginId; 
                            ";

            strSql = string.Format(strSql, strLoginId);
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectClients");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 处理客户
        [WebMethod]
        public string ContactClients(string strLanId, string strType, string strLoginId, string strTargetId)
        {
            string strSql = @"INSERT INTO T_Concern
                               ([type]
                               ,targetID
                               ,tauloginid
                               ,rowstatus
                               ,create_time)
                                VALUES
                               ('{0}','{1}','{2}',0,getdate())";

            strSql = string.Format(strSql, strType, strTargetId, strLoginId);

            new DALTransaction().ExecuteQuerySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - ContactClients");
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 查询客户关怀联系记录
        [WebMethod]
        public string SelectConcern(string strLanId, string strType, string strLoginId, string strTargetId)
        {
//            string strSql = @"INSERT INTO T_Concern
//                               ([type]
//                               ,targetID
//                               ,tauloginid
//                               ,rowstatus
//                               ,create_time)
//                                VALUES
//                               ('{0}','{1}','{2}',0,getdate())";

//            strSql = string.Format(strSql, strType, strTargetId, strLoginId);

//            new DALTransaction().ExecuteQuerySQLText(strSql);

            Dictionary<string, object> Parm = new Dictionary<string, object>();
            Parm.Add("@LoginId", strLoginId);
            Parm.Add("@TargetId", strTargetId);
            Parm.Add("@Type", strType);

            DataTable dt = new DALTransaction().GetDataTableBySPName("SP_SelectConcern", Parm);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - ContactClients");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 更改客户标签
        [WebMethod]
        public string EditClients(string strLanId, string strLoginId,string strLevel)
        {
            string strSql = @"update T_AppUser set tauAdditional4='{0}' where tauLoginId='{1}')";

            strSql = string.Format(strSql, strLevel, strLoginId);
            new DALTransaction().ExecuteQuerySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectClients");
            
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion

        #region 分供商提交异常
        [WebMethod]
        public string CreateException(string strLanId, T_Exception Exception)
        {
            string strSql = @"INSERT INTO T_Exception
                               (ContractID
                               ,type
                               ,Content
                               ,isread
                               ,rowstatus
                               ,create_time)
                                VALUES
                               ('{0}','{1}','{2}',0,0,getdate())";

            strSql = string.Format(strSql, Exception.ContractID, Exception.type, Exception.Content);
            
            new DALTransaction().ExecuteQuerySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - EditSupplyRange");
            return JsonConvert.SerializeObject(jRtn);
        }

        #endregion

        #region 查询异常
        [WebMethod]
        public string SelectException(string strLanId, string strContractId, string strExid)
        {
            string strSql = "";
            if (!string.IsNullOrWhiteSpace(strContractId))
            {
                strSql = @"select t1.*,t2.title 
                        from T_Contract t
                        inner join T_Exception t1 on t.ContractID=t1.ContractID
                        inner join T_Request t2 on t.RequestID=t2.RequestID
                        where t.ContractID='{0}' 
                        order by t1.create_time desc";

                strSql = string.Format(strSql, strContractId);
            }

            if (!string.IsNullOrWhiteSpace(strExid))
            {
                strSql = @"select t1.*
                        from T_Exception t1
                        where t1.Exid='{0}'
                        order by t1.create_time desc";

                strSql = string.Format(strSql, strExid);
            }

            
            DataTable dt = new DALTransaction().GetDataTableBySQLText(strSql);

            //获取多语言提示
            string strTips = _commonhelp.GetTips("1", strLanId);
            JObject jRtn = new JObject();
            jRtn = Common.Return("0", strTips, "AshxPortalRequest - SelectContract");
            jRtn["data"] = JsonConvert.SerializeObject(dt);
            return JsonConvert.SerializeObject(jRtn);
        }
        #endregion
        
        #endregion

        #region 公共方法
        /// <summary>
        /// 直接登录，内部调用
        /// </summary>
        /// <param name="strTauLoginId"></param>
        /// <param name="strTauPwd"></param>
        /// <returns></returns>
        private DataTable GetUserInfo(string strTauLoginId, string strTauPwd)
        {
            string strUrl = GetUrl();

            string strSql = @"select t1.*,t3.ticName,t3.ticDesc,'{2}'+t3.ticPictureUrl ticPictureUrl 
                            from T_AppUser t1
                            left join T_AppUserAuthorised t2 on t1.TAUID=t2.tauaTauId
                            left join T_InformationCategory t3 on t2.tauaTicId=t3.TICID
                            where t1.tauRowStatus=0 --and (t3.ticRowStatus=0 or t3.ticRowStatus is null)
                            and t1.tauLoginId='{0}' and t1.tauPsw='{1}'";

            strSql = string.Format(strSql, strTauLoginId, strTauPwd, strUrl);
            return new DALTransaction().GetDataTableBySQLText(strSql);
        }


        private void UpdateSupplyCode(string strCode)
        {
            string strSql = @"update T_SupplyCode set IsUsed=1 where Code='{0}'";

            strSql = string.Format(strSql, strCode);
            new DALTransaction().ExecuteQuerySQLText(strSql);
        }

        /// <summary>
        /// 解析上传的附件：type：1.excel；2.图片用| 分格多张图片
        /// </summary>
        /// <param name="strBase64"></param>
        /// <param name="strType"></param>
        /// <returns></returns>
        private string UpdateExcelOrImg(string strBase64, string strType)
        {

            string SaveImageFolder = @"D:\Deploy\AnTeWei\Interface\UploadFile";
            try
            {
                if (!Directory.Exists(SaveImageFolder))
                    Directory.CreateDirectory(SaveImageFolder);
                string sFileName = "";
                string strFileName = "";
                if (strType == "2")
                {
                    //string[] split = strBase64.Split(new Char[] { '|' });
                    string[] split = strBase64.Split(new Char[] {'|'});
                    int intCount = split.Length;
                    
                    //根据图片张数，进行不同操作
                    if (intCount == 0)
                    {
                        return "";
                    }
                    else if (intCount == 1)
                    {
                        Log.Debug("Moose20160401", "只有一个");
                        string temp = strBase64;
                        temp = temp.Replace(" ", "+");
                        byte[] arr = Convert.FromBase64String(temp);

                        MemoryStream ms = new MemoryStream(arr);
                        Bitmap bmp = new Bitmap(ms);
                        Random rd = new Random();

                        sFileName = string.Format("{0}.png", DateTime.Now.ToString("yyyyMMddHHmmssfff") + rd.Next(100, 999));
                        bmp.Save(SaveImageFolder + "\\" + sFileName, ImageFormat.Png);

                        
                        //System.Drawing.Image imag = byteArrayToImage(BitmapToBytes(bmp));

                        //Bitmap bmp_s = PercentImage(imag, 0.5);
                        //bmp_s.Save(SaveImageFolder + "\\" +  sFileName, ImageFormat.Png);

                        ms.Close();

                        return sFileName;
                    }
                    else
                    {
                        Log.Debug("Moose20160401多图", split.Length.ToString());
                        foreach (var i in split)
                        {
                            string temp = i;
                            temp = temp.Replace(" ", "+");
                            byte[] arr = Convert.FromBase64String(temp);

                            MemoryStream ms = new MemoryStream(arr);
                            Bitmap bmp = new Bitmap(ms);
                            Random rd = new Random();

                            sFileName = string.Format("{0}.png", DateTime.Now.ToString("yyyyMMddHHmmssfff") + rd.Next(100, 999));
                            bmp.Save(SaveImageFolder + "\\" + sFileName, ImageFormat.Png);

                            
                            //System.Drawing.Image imag = byteArrayToImage(BitmapToBytes(bmp));

                            //Bitmap bmp_s = PercentImage(imag, 0.5);
                            //bmp_s.Save(SaveImageFolder + "\\" + sFileName, ImageFormat.Png);
                            strFileName = strFileName + "|" + sFileName;
                            Log.Debug("检查img名字", strFileName);
                            ms.Close();
                        }

                        return strFileName;
                    }

                    

                }
                else
                {
                    string temp = strBase64;
                    temp = temp.Replace(" ", "+");
                    byte[] arr = Convert.FromBase64String(temp);

                    Random rd = new Random();
                    sFileName = string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmssfff") + rd.Next(100, 999));

                    FileStream fs = new FileStream(SaveImageFolder + "\\" + sFileName, FileMode.Create, FileAccess.Write);
                    fs.Write(arr, 0, arr.Length);
                    fs.Flush();
                    fs.Close();

                    return sFileName;
                }

                
            }
            catch (Exception ex)
            {
                Log.Debug("上传图片", ex.Message.ToString());
                return "";
            }
        }

        /// <summary>
        /// 按比例压缩照片尺寸
        /// </summary>
        /// <param name="srcImage"></param>
        /// <param name="percent"></param>
        /// <returns></returns>
        public Bitmap PercentImage(Image srcImage, double percent)
        {
            // 缩小后的高度 
            int newH = int.Parse(Math.Round(srcImage.Height * percent).ToString());
            // 缩小后的宽度 
            int newW = int.Parse(Math.Round(srcImage.Width * percent).ToString());
            try
            {
                // 要保存到的图片 
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                // 插值算法的质量 
                g.InterpolationMode = InterpolationMode.Default;
                g.DrawImage(srcImage, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, srcImage.Width, srcImage.Height), GraphicsUnit.Pixel);
                g.Dispose();
                return b;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public byte[] BitmapToBytes(Bitmap Bitmap)
        {
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream();
                Bitmap.Save(ms, Bitmap.RawFormat);
                byte[] byteImage = new Byte[ms.Length];
                byteImage = ms.ToArray();
                return byteImage;
            }
            catch (ArgumentNullException ex)
            {
                throw ex;
            }
            finally
            {
                ms.Close();
            }
        }

        /// <summary>
        /// byte[]转换成Image
        /// </summary>
        /// <param name="byteArrayIn">二进制图片流</param>
        /// <returns>Image</returns>
        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn == null)
                return null;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
            {
                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                ms.Flush();
                return returnImage;
            }
        }


        private static string GetUrl()
        {
            string strUrl = "";
            strUrl = ConfigurationManager.AppSettings["UploadFileUrl"];

            return strUrl;
        }

        /// <summary>
        /// 检查该报价单是否未中标
        /// </summary>
        /// <param name="strResponeId"></param>
        /// <returns></returns>
        private bool CheckRespone(string strResponeId)
        {
            DataTable dtConfirmed = new DALTransaction().GetDataTableBySQLText(string.Format(@"select COUNT(*) ConfirmedCount from T_ResponseDetail where ResponseID='{0}' and Is_Confirmed=1", strResponeId));

            int ConfirmedCount = Convert.ToInt32(dtConfirmed.Rows[0]["ConfirmedCount"].ToString());

            if (ConfirmedCount == 0)
            {
                string strSql = @"update T_Response set rowstatus=8
                            where ResponseID='{0}'";
                strSql = string.Format(strSql, strResponeId);
                new DALTransaction().ExecuteQuerySQLText(strSql);
                return true;
            }
            else
            {
                return false;
            }

        }

        
        #endregion
    }


}
